﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMALLERP.ComClass;
using SMALLERP.DataClass;
using System.Data.SqlClient;
using XJLSC.PM;
using XJLSC.QM;
using System.Collections.Specialized;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.IO.Compression;
using System.Net;
using System.Data.OleDb;
using System.Collections;
using System.Reflection;
using DevExpress.XtraGrid.Columns;

namespace SMALLERP.PM
{
    public partial class ZPCJH : Form
    {   
        private readonly DataBase db = new DataBase();
        private SqlDataReader sdr1;
        //private HashSet<VcLink> myVisibleLinks = new HashSet<VcLink>();
        //private VcGroupModificationTypes myGroupModificationType = VcGroupModificationTypes.vcGMTNothing;
        private int myNoOfExpandedGroups = 0;
        SCKC f;
        GZZXKC k;
        LXPCCS j;
        private Boolean isUpdate = false;
        public double dqsl = 0;
        //double dpsl = 0;    //待排数量
        //double dbsl = 0;    //当班产量
        public int row = 0;
        public int column = 0;
        public ZPCJH()
        {
            InitializeComponent();
        }

        private void ZPCJH_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();//建立个数据表
            textBox1.Text = DateTime.Now.ToShortDateString().ToString();
            textBox3.Text = DateTime.Now.AddDays(30).ToShortDateString().ToString();
            //dt.Columns.Add(new DataColumn("id", typeof(int)));//在表中添加int类型的列

            //dt.Columns.Add(new DataColumn("Name", typeof(string)));//在表中添加string类型的Name列

            //如果要添加一个textbox效果的列，可做如下处理

            //dt.Columns.Add(new DataColumn("选中", typeof(bool)));


            //this.gridView1.BestFitColumns();//自动调整所有字段宽度
            this.WindowState = FormWindowState.Maximized;


        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            textBox1.Text = dateTimePicker1.Value.ToShortDateString();
            if (textBox3.Text != "")
            {
              
                for (int i= gridView1.Columns.Count - 1; i>0;i--)
                {
                    if(gridView1.Columns[i].Name=="scrq")
                    {
                        gridView1.Columns.RemoveAt(i);
                    }
                }

                TimeSpan ts = Convert.ToDateTime(textBox3.Text) - Convert.ToDateTime(textBox1.Text);
                int k = ts.Days;
                if (k >= 0)
                {
                    DateTime rq = Convert.ToDateTime(textBox1.Text);

                    DevExpress.XtraGrid.Columns.GridColumn col = null;
                    for (int i = 0; i <= k; i++)
                    {

                        rq = Convert.ToDateTime(textBox1.Text).AddDays(i);
                        string str = rq.ToString("MM月dd日");
                        string str1 = rq.ToString("yyyyMMdd");
                        col = gridView1.Columns.AddVisible("scrq");
                        col.Name = "scrq";
                        col.FieldName = str1;
                        col.Caption = str;
                        col.MinWidth = 80;
                        col.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
                        col.SummaryItem.DisplayFormat = "{0:0.##}";
                        col.OptionsColumn.AllowEdit = true;
                        this.gridView1.Columns.Add(col);

                        //dt.Columns.Add(new DataColumn(Convert.ToString(str), typeof(string)));

                    }
                 
                }
                else
                {
                    MessageBox.Show("起始日期不能小于截止日期", "提示");
                    textBox1.Text = textBox3.Text;
                }
            }


        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            textBox3.Text = dateTimePicker2.Value.ToShortDateString();
            if (textBox1.Text != "")
            {
                for (int i = gridView1.Columns.Count - 1; i > 0; i--)
                {
                    if (gridView1.Columns[i].Name == "scrq")
                    {
                        gridView1.Columns.RemoveAt(i);
                    }
                }

                TimeSpan ts = Convert.ToDateTime(textBox3.Text) - Convert.ToDateTime(textBox1.Text);
                int k = ts.Days;
                if (k >= 0)
                {
                    //DataTable dt = new DataTable();//建立个数据表

                    DateTime rq = Convert.ToDateTime(textBox1.Text);
                    DevExpress.XtraGrid.Columns.GridColumn col = null;
                    for (int i = 0; i <= k; i++)
                    {
                    rq = Convert.ToDateTime(textBox1.Text).AddDays(i);
                    string str = rq.ToString("MM月dd日");
                    string str1 = rq.ToString("yyyyMMdd");
                    col = gridView1.Columns.AddVisible("scrq");
                    col.Name = "scrq";
                    col.FieldName = str1;
                    col.Caption = str;
                    col.MinWidth = 80;
                    col.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
                    col.SummaryItem.DisplayFormat = "{0:0.##}";
                    col.OptionsColumn.AllowEdit = true;
                    this.gridView1.Columns.Add(col);

                    }
                    //dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("起始日期不能小于截止日期", "提示");
                    textBox3.Text = textBox1.Text;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string strSql = "";
                if (checkBox1.CheckState == CheckState.Checked)
                {
                    if (checkBox2.CheckState == CheckState.Unchecked)
                    {
                        //strSql = "declare @sql1 VARCHAR(8000) set @sql1 = 'SELECT DISTINCT A.TA001 AS 工单别,A.TA002 AS 工单号,RTRIM(A.TA006) AS 产品品号,MB002 AS 品名,MB003  AS 规格,MF003 AS 工序号,ISNULL(MF004,''****'') AS 工艺,MW002 AS 工艺名称,ISNULL(TD013,DDJQ) 订单交期,TD020 订单备注,CEILING(A.TA015) AS 数量,ISNULL(CEILING(D.TY004),0) AS 已排产量,CEILING(A.TA017) AS 已入库量,CEILING(A.TA015-A.TA017) AS 未生产量,RTRIM(A.TA021) 工作中心,'''' 生产线名称,CEILING(RC) 班产量,RTRIM(C.TY020) 类别,CEILING(ISNULL(MC007,0)) AS 库存量,ISNULL((SELECT ISNULL(SUM(XQL),0) FROM V_XQL WHERE TY003>=CONVERT(CHAR(8),GETDATE(),112) AND RTRIM(TB003)=RTRIM(A.TA006)),0) AS 总需求量,ISNULL(F.QTL,0) AS 齐套率,RTRIM(A.TA083) 生产线' select @sql1 = @sql1 + ' , max(case TY003 when ''' + TY003 + ''' then CEILING(C.TY004) else 0 end) [' + TY003 + ']' from (SELECT DISTINCT RQ001 AS TY003 FROM CMSRQ WHERE RQ001>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox1.Text + "'),112) AND RQ001<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox3.Text + "'),112)) as a set @sql1 = @sql1 + ' FROM MOCTA A LEFT JOIN COPTD ON A.TA026=COPTD.TD001 AND A.TA027=COPTD.TD002 AND A.TA028=COPTD.TD003 LEFT JOIN INVMB ON A.TA006=MB001 LEFT JOIN V_DDJQ ON TA027=DDH LEFT JOIN BOMMF ON MB010=MF001 AND MB011=MF002 LEFT JOIN V_GXYPC D ON A.TA001=D.TY001 AND A.TA002=D.TY002 AND ISNULL(MF004,''****'')=D.TY019 LEFT JOIN MOCTY C ON A.TA001=C.TY001  AND A.TA002=C.TY002 AND RTRIM(D.TY019)=RTRIM(C.TY019)  LEFT JOIN CMSMX ON RTRIM(MX001)=RTRIM(C.TY009)  LEFT JOIN GXSB ON A.TA006=PH  AND C.TY009=SB LEFT JOIN V_CKKC ON A.TA006=MC001 LEFT JOIN V_QTL F ON A.TA001=F.TB001 AND A.TA002=F.TB002  AND ISNULL(MF004,''****'')=F.TB006 LEFT JOIN CMSMW ON MF004=MW001";
                        //strSql += " WHERE TA011<>''y'' AND TA011<>''Y'' AND (A.TA006 LIKE ''" + textBox7.Text + "%'' OR ''" + textBox7.Text + "''='''') AND (TD020 like ''%" + textBox6.Text + "%'' OR ''" + textBox6.Text + "''='''') AND (A.TA002 LIKE ''%" + textBox4.Text + "%'' OR ''" + textBox4.Text + "''='''') AND (INVMB.MB002 LIKE ''%" + textBox5.Text + "%'' OR INVMB.MB003 LIKE ''%" + textBox5.Text + "%'' OR ''" + textBox5.Text + "''='''') AND ISNULL(MW002,'''') LIKE ''%" + textBox9.Text.ToString() + "%'' AND (C.TY009 LIKE ''%" + textBox8.Text + "%'' OR ''" + textBox8.Text + "''='''') group by A.TA001,A.TA002,COPTD.TD020,RTRIM(A.TA006),MB002,MB003,A.TA015,D.TY004,A.TA017,RC,RTRIM(C.TY009),RTRIM(C.TY020),ISNULL(MC007,0),QTL,A.TA021,MF003,ISNULL(MF004,''****''),ISNULL(TD013,DDJQ),MW002,A.TA083 ORDER BY TA001,TA002,MF003' EXEC(@sql1)";
                        strSql = "declare @sql1 VARCHAR(8000) set @sql1 = 'SELECT SUBSTRING(A.DOC_NO, 1, CHARINDEX(''-'', A.DOC_NO, 1) - 1) AS 工单别, SUBSTRING(A.DOC_NO, CHARINDEX(''-'', A.DOC_NO, 1) + 1, LEN(A.DOC_NO) - 5) 工单号,RTRIM(ITEM.ITEM_CODE) AS 产品品号,RTRIM(A.UDF021) AS 订单号,ITEM.ITEM_NAME AS 品名,ITEM.ITEM_SPECIFICATION+'';''+RTRIM(A.UDF023)  AS 规格,OPERATION_SEQ 工序号,ISNULL(OPERATION_CODE,''****'') 工艺,ISNULL(OPERATION_NAME,'''') 工艺名称,ISNULL(A.PLAN_START_DATE,DDJQ) 预计开工日,A.REMARK 订单备注,CEILING(A.PLAN_QTY) AS 数量,ISNULL(CEILING(D.TY004),0) AS 已排产量,CEILING(ISNULL(D.TY006,0)) AS 已报工量,CEILING(A.PLAN_QTY-CEILING(ISNULL(D.TY006,0))) AS 未生产量,RTRIM(ISNULL(WORK_CENTER.WORK_CENTER_CODE,'''')) 工作中心,'''' 生产线名称,CEILING(ISNULL(MC007,0)) AS 库存量,ISNULL((SELECT CAST(ISNULL(SUM(XQL),0) AS REAL) FROM V_XQL WHERE TY003>=CONVERT(CHAR(8),GETDATE(),112) AND RTRIM(TB003)=RTRIM(ITEM.ITEM_CODE)),0) AS 总需求量,ISNULL(F.QTL,0) AS 齐套率,ISNULL(PROJECT_CODE,'''') 生产线' select @sql1 = @sql1 + ' , max(case TY003 when ''' + TY003 + ''' then CEILING(ISNULL(C.TY004,0)) else 0 end) [' + TY003 + ']' from (SELECT DISTINCT RQ001 AS TY003 FROM CMSRQ WHERE RQ001>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox1.Text + "'),112) AND RQ001<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox3.Text + "'),112)) as a set @sql1 = @sql1 + ' FROM MO A LEFT JOIN ITEM ON A.ITEM_ID=ITEM.ITEM_BUSINESS_ID LEFT JOIN V_DDJQ ON A.UDF021=DDH LEFT JOIN ITEM_ROUTING ON A.ITEM_ID=ITEM_ROUTING.ITEM_ID LEFT JOIN ITEM_ROUTING_D ON ITEM_ROUTING_D.ITEM_ROUTING_ID=ITEM_ROUTING.ITEM_ROUTING_ID LEFT JOIN OPERATION ON ITEM_ROUTING_D.OPERATION_ID=OPERATION.OPERATION_ID LEFT JOIN V_GXYPC D ON SUBSTRING(A.DOC_NO, 1, CHARINDEX(''-'', A.DOC_NO, 1) - 1)=D.TY001 AND SUBSTRING(A.DOC_NO, CHARINDEX(''-'', A.DOC_NO, 1) + 1, LEN(A.DOC_NO) - 5)=D.TY002 AND ISNULL(OPERATION_SEQ,''****'')=D.TY019  LEFT JOIN MOCTY C ON SUBSTRING(A.DOC_NO, 1, CHARINDEX(''-'', A.DOC_NO, 1) - 1)=C.TY001  AND SUBSTRING(A.DOC_NO, CHARINDEX(''-'', A.DOC_NO, 1) + 1, LEN(A.DOC_NO) - 5)=C.TY002  AND (RTRIM(D.TY019)=RTRIM(C.TY019) OR D.TY019=''****'')   LEFT JOIN CMSMX ON RTRIM(MX001)=RTRIM(C.TY009) AND RTRIM(D.TY019)=RTRIM(C.TY019)  LEFT JOIN GXSB ON ITEM.ITEM_CODE=PH  AND C.TY009=SB  LEFT JOIN V_CKKC ON ITEM.ITEM_CODE=MC001 LEFT JOIN V_QTL F ON SUBSTRING(A.DOC_NO, 1, CHARINDEX(''-'', A.DOC_NO, 1) - 1)=F.TB001 AND SUBSTRING(A.DOC_NO, CHARINDEX(''-'', A.DOC_NO, 1) + 1, LEN(A.DOC_NO) - 5)=F.TB002 AND ISNULL(OPERATION_CODE,''****'')=F.TB006 LEFT JOIN WORK_CENTER ON A.SOURCE_ID_ROid=WORK_CENTER.WORK_CENTER_ID LEFT JOIN PROJECT ON A.PROJECT_ID=PROJECT.PROJECT_ID";
                        strSql += " WHERE A.[STATUS]<>''y'' AND A.[STATUS]<>''Y'' AND A.[ApproveStatus]=''Y''  AND (ITEM.ITEM_CODE LIKE ''" + textBox7.Text + "%'' OR ''" + textBox7.Text + "''='''') AND (A.UDF021 like ''%" + textBox6.Text + "%'' OR ''" + textBox6.Text + "''='''') AND (SUBSTRING(A.DOC_NO, CHARINDEX(''-'', A.DOC_NO, 1) + 1, LEN(A.DOC_NO) - 5) LIKE ''%" + textBox4.Text + "%'' OR ''" + textBox4.Text + "''='''') AND (ITEM.ITEM_NAME LIKE ''%" + textBox5.Text + "%'' OR ITEM.ITEM_SPECIFICATION LIKE ''%" + textBox5.Text + "%'' OR ''" + textBox5.Text + "''='''') AND (C.TY009 LIKE ''%" + textBox8.Text + "%'' OR ''" + textBox8.Text + "''='''') AND A.PLAN_QTY-A.COMPLETED_QTY>0 group by A.DOC_NO,A.REMARK,RTRIM(ITEM.ITEM_CODE),RTRIM(A.UDF021),ITEM.ITEM_NAME,ITEM.ITEM_SPECIFICATION,A.PLAN_QTY,A.COMPLETED_QTY,D.TY004,D.TY006,RC,RTRIM(C.TY009),RTRIM(C.TY020),ISNULL(MC007,0),QTL,WORK_CENTER.WORK_CENTER_CODE,ISNULL(A.PLAN_START_DATE,DDJQ),A.UDF023,OPERATION_SEQ,ISNULL(OPERATION_CODE,''****''),ISNULL(OPERATION_NAME,''''),ISNULL(PROJECT_CODE,'''') ORDER BY A.DOC_NO,OPERATION_SEQ' EXEC(@sql1)";
                    
                    }
                    if (checkBox2.CheckState == CheckState.Checked)
                    {
                        //strSql = "declare @sql1 VARCHAR(8000) set @sql1 = 'SELECT DISTINCT A.TA001 AS 工单别,A.TA002 AS 工单号,RTRIM(A.TA006) AS 产品品号,MB002 AS 品名,MB003  AS 规格,MF003 AS 工序号,ISNULL(MF004,''****'') AS 工艺,MW002 AS 工艺名称,ISNULL(TD013,DDJQ) 订单交期,TD020 订单备注,CEILING(A.TA015) AS 数量,ISNULL(CEILING(D.TY004),0) AS 已排产量,CEILING(A.TA017) AS 已入库量,CEILING(A.TA015-A.TA017) AS 未生产量,RTRIM(A.TA021) 工作中心,'''' 生产线名称,CEILING(RC) 班产量,RTRIM(C.TY020) 类别,CEILING(ISNULL(MC007,0)) AS 库存量,ISNULL((SELECT ISNULL(SUM(XQL),0) FROM V_XQL WHERE TY003>=CONVERT(CHAR(8),GETDATE(),112) AND RTRIM(TB003)=RTRIM(A.TA006)),0) AS 总需求量,ISNULL(F.QTL,0) AS 齐套率,RTRIM(A.TA083) 生产线' select @sql1 = @sql1 + ' , max(case TY003 when ''' + TY003 + ''' then CEILING(C.TY004) else 0 end) [' + TY003 + ']' from (SELECT DISTINCT RQ001 AS TY003 FROM CMSRQ WHERE RQ001>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox1.Text + "'),112) AND RQ001<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox3.Text + "'),112)) as a set @sql1 = @sql1 + ' FROM MOCTA A LEFT JOIN COPTD ON A.TA026=COPTD.TD001 AND A.TA027=COPTD.TD002 AND A.TA028=COPTD.TD003 LEFT JOIN INVMB ON A.TA006=MB001 LEFT JOIN V_DDJQ ON TA027=DDH LEFT JOIN BOMMF ON MB010=MF001 AND MB011=MF002 LEFT JOIN V_GXYPC D ON A.TA001=D.TY001 AND A.TA002=D.TY002 AND ISNULL(MF004,''****'')=D.TY019 LEFT JOIN MOCTY C ON A.TA001=C.TY001  AND A.TA002=C.TY002 AND RTRIM(D.TY019)=RTRIM(C.TY019)  LEFT JOIN CMSMX ON RTRIM(MX001)=RTRIM(C.TY009)  LEFT JOIN GXSB ON A.TA006=PH  AND C.TY009=SB LEFT JOIN V_CKKC ON A.TA006=MC001 LEFT JOIN V_QTL F ON A.TA001=F.TB001 AND A.TA002=F.TB002  AND ISNULL(MF004,''****'')=F.TB006 LEFT JOIN CMSMW ON MF004=MW001";
                        //strSql += " WHERE ISNULL(CEILING(D.TY004),0)>0 AND TA011<>''y'' AND TA011<>''Y'' AND (A.TA006 LIKE ''" + textBox7.Text + "%'' OR ''" + textBox7.Text + "''='''') AND (TD020 like ''%" + textBox6.Text + "%'' OR ''" + textBox6.Text + "''='''') AND (A.TA002 LIKE ''%" + textBox4.Text + "%'' OR ''" + textBox4.Text + "''='''') AND (INVMB.MB002 LIKE ''%" + textBox5.Text + "%'' OR INVMB.MB003 LIKE ''%" + textBox5.Text + "%'' OR ''" + textBox5.Text + "''='''') AND ISNULL(MW002,'''') LIKE ''%" + textBox9.Text.ToString() + "%'' AND (C.TY009 LIKE ''%" + textBox8.Text + "%'' OR ''" + textBox8.Text + "''='''') group by A.TA001,A.TA002,COPTD.TD020,RTRIM(A.TA006),MB002,MB003,A.TA015,D.TY004,A.TA017,RC,RTRIM(C.TY009),RTRIM(C.TY020),ISNULL(MC007,0),QTL,A.TA021,MF003,ISNULL(MF004,''****''),ISNULL(TD013,DDJQ),MW002,A.TA083 ORDER BY TA001,TA002,MF003' EXEC(@sql1)";
                        strSql = "declare @sql1 VARCHAR(8000) set @sql1 = 'SELECT SUBSTRING(A.DOC_NO, 1, CHARINDEX(''-'', A.DOC_NO, 1) - 1) AS 工单别, SUBSTRING(A.DOC_NO, CHARINDEX(''-'', A.DOC_NO, 1) + 1, LEN(A.DOC_NO) - 5) 工单号,RTRIM(ITEM.ITEM_CODE) AS 产品品号,RTRIM(A.UDF021) AS 订单号,ITEM.ITEM_NAME AS 品名,ITEM.ITEM_SPECIFICATION+'';''+RTRIM(A.UDF023)  AS 规格,OPERATION_SEQ 工序号,ISNULL(OPERATION_CODE,''****'') 工艺,ISNULL(OPERATION_NAME,'''') 工艺名称,ISNULL(A.PLAN_START_DATE,DDJQ) 预计开工日,A.REMARK 订单备注,CEILING(A.PLAN_QTY) AS 数量,ISNULL(CEILING(D.TY004),0) AS 已排产量,CEILING(ISNULL(D.TY006,0)) AS 已报工量,CEILING(A.PLAN_QTY-CEILING(ISNULL(D.TY006,0))) AS 未生产量,RTRIM(ISNULL(WORK_CENTER.WORK_CENTER_CODE,'''')) 工作中心,'''' 生产线名称,CEILING(ISNULL(MC007,0)) AS 库存量,ISNULL((SELECT CAST(ISNULL(SUM(XQL),0) AS REAL) FROM V_XQL WHERE TY003>=CONVERT(CHAR(8),GETDATE(),112) AND RTRIM(TB003)=RTRIM(ITEM.ITEM_CODE)),0) AS 总需求量,ISNULL(F.QTL,0) AS 齐套率,ISNULL(PROJECT_CODE,'''') 生产线' select @sql1 = @sql1 + ' , max(case TY003 when ''' + TY003 + ''' then CEILING(ISNULL(C.TY004,0)) else 0 end) [' + TY003 + ']' from (SELECT DISTINCT RQ001 AS TY003 FROM CMSRQ WHERE RQ001>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox1.Text + "'),112) AND RQ001<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox3.Text + "'),112)) as a set @sql1 = @sql1 + ' FROM MO A LEFT JOIN ITEM ON A.ITEM_ID=ITEM.ITEM_BUSINESS_ID LEFT JOIN V_DDJQ ON A.UDF021=DDH LEFT JOIN ITEM_ROUTING ON A.ITEM_ID=ITEM_ROUTING.ITEM_ID LEFT JOIN ITEM_ROUTING_D ON ITEM_ROUTING_D.ITEM_ROUTING_ID=ITEM_ROUTING.ITEM_ROUTING_ID LEFT JOIN OPERATION ON ITEM_ROUTING_D.OPERATION_ID=OPERATION.OPERATION_ID LEFT JOIN V_GXYPC D ON SUBSTRING(A.DOC_NO, 1, CHARINDEX(''-'', A.DOC_NO, 1) - 1)=D.TY001 AND SUBSTRING(A.DOC_NO, CHARINDEX(''-'', A.DOC_NO, 1) + 1, LEN(A.DOC_NO) - 5)=D.TY002 AND ISNULL(OPERATION_SEQ,''****'')=D.TY019 LEFT JOIN MOCTY C ON SUBSTRING(A.DOC_NO, 1, CHARINDEX(''-'', A.DOC_NO, 1) - 1)=C.TY001  AND SUBSTRING(A.DOC_NO, CHARINDEX(''-'', A.DOC_NO, 1) + 1, LEN(A.DOC_NO) - 5)=C.TY002  AND (RTRIM(D.TY019)=RTRIM(C.TY019) OR D.TY019=''****'')  LEFT JOIN CMSMX ON RTRIM(MX001)=RTRIM(C.TY009) AND RTRIM(D.TY019)=RTRIM(C.TY019)  LEFT JOIN GXSB ON ITEM.ITEM_CODE=PH  AND C.TY009=SB  LEFT JOIN V_CKKC ON ITEM.ITEM_CODE=MC001 LEFT JOIN V_QTL F ON SUBSTRING(A.DOC_NO, 1, CHARINDEX(''-'', A.DOC_NO, 1) - 1)=F.TB001 AND SUBSTRING(A.DOC_NO, CHARINDEX(''-'', A.DOC_NO, 1) + 1, LEN(A.DOC_NO) - 5)=F.TB002 AND ISNULL(OPERATION_CODE,''****'')=F.TB006 LEFT JOIN WORK_CENTER ON A.SOURCE_ID_ROid=WORK_CENTER.WORK_CENTER_ID LEFT JOIN PROJECT ON A.PROJECT_ID=PROJECT.PROJECT_ID";
                        strSql += " WHERE ISNULL(CEILING(D.TY004),0)>0 AND A.[STATUS]<>''y'' AND A.[STATUS]<>''Y'' AND A.[ApproveStatus]=''Y'' AND (ITEM.ITEM_CODE LIKE ''" + textBox7.Text + "%'' OR ''" + textBox7.Text + "''='''') AND (A.UDF021 like ''%" + textBox6.Text + "%'' OR ''" + textBox6.Text + "''='''') AND (SUBSTRING(A.DOC_NO, CHARINDEX(''-'', A.DOC_NO, 1) + 1, LEN(A.DOC_NO) - 5) LIKE ''%" + textBox4.Text + "%'' OR ''" + textBox4.Text + "''='''') AND (ITEM.ITEM_NAME LIKE ''%" + textBox5.Text + "%'' OR ITEM.ITEM_SPECIFICATION LIKE ''%" + textBox5.Text + "%'' OR ''" + textBox5.Text + "''='''') AND (C.TY009 LIKE ''%" + textBox8.Text + "%'' OR ''" + textBox8.Text + "''='''') AND A.PLAN_QTY-A.COMPLETED_QTY>0 group by A.DOC_NO,A.REMARK,RTRIM(ITEM.ITEM_CODE),RTRIM(A.UDF021),ITEM.ITEM_NAME,ITEM.ITEM_SPECIFICATION,A.PLAN_QTY,A.COMPLETED_QTY,D.TY004,D.TY006,RC,RTRIM(C.TY009),RTRIM(C.TY020),ISNULL(MC007,0),QTL,WORK_CENTER.WORK_CENTER_CODE,ISNULL(A.PLAN_START_DATE,DDJQ),A.UDF023,OPERATION_SEQ,ISNULL(OPERATION_CODE,''****''),ISNULL(OPERATION_NAME,''''),ISNULL(PROJECT_CODE,'''') ORDER BY A.DOC_NO,OPERATION_SEQ' EXEC(@sql1)";
                    
                    }
                }
                if (checkBox1.CheckState == CheckState.Unchecked)
                {
                    if (checkBox2.CheckState == CheckState.Unchecked)
                    {
                        //strSql = "declare @sql1 VARCHAR(8000) set @sql1 = 'SELECT A.TA001 AS 工单别,A.TA002 AS 工单号,RTRIM(A.TA006) AS 产品品号,RTRIM(TD020) AS 订单号,MB002 AS 品名,MB003  AS 规格,MF003 AS 工序号,ISNULL(MF004,''****'') AS 工艺,MW002 AS 工艺名称,ISNULL(TD013,DDJQ) 订单交期,TD020 订单备注,CEILING(A.TA015) AS 数量,ISNULL(CEILING(D.TY004),0) AS 已排产量,CEILING(A.TA017) AS 已入库量,CEILING(A.TA015-A.TA017) AS 未生产量,RTRIM(A.TA021) 工作中心,'''' 生产线名称,CEILING(RC) 班产量,CEILING(ISNULL(MC007,0)) AS 库存量,ISNULL((SELECT ISNULL(SUM(XQL),0) FROM V_XQL WHERE TY003>=CONVERT(CHAR(8),GETDATE(),112) AND RTRIM(TB003)=RTRIM(A.TA006)),0) AS 总需求量,ISNULL(F.QTL,0) AS 齐套率,RTRIM(A.TA083) 生产线' select @sql1 = @sql1 + ' , max(case TY003 when ''' + TY003 + ''' then CEILING(ISNULL(C.TY004,0)) else 0 end) [' + TY003 + ']' from (SELECT DISTINCT RQ001 AS TY003 FROM CMSRQ WHERE RQ001>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox1.Text + "'),112) AND RQ001<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox3.Text + "'),112)) as a set @sql1 = @sql1 + ' FROM MOCTA A LEFT JOIN COPTD ON A.TA026=COPTD.TD001 AND A.TA027=COPTD.TD002 AND A.TA028=COPTD.TD003 LEFT JOIN INVMB ON A.TA006=MB001 LEFT JOIN V_DDJQ ON TA027=DDH  LEFT JOIN BOMMF ON MB010=MF001 AND MB011=MF002 LEFT JOIN V_GXYPC D ON A.TA001=D.TY001 AND A.TA002=D.TY002 AND ISNULL(MF004,''****'')=D.TY019  LEFT JOIN MOCTY C ON A.TA001=C.TY001  AND A.TA002=C.TY002  AND RTRIM(D.TY019)=RTRIM(C.TY019)  LEFT JOIN CMSMX ON RTRIM(MX001)=RTRIM(C.TY009) AND RTRIM(D.TY019)=RTRIM(C.TY019)  LEFT JOIN GXSB ON A.TA006=PH  AND C.TY009=SB  LEFT JOIN V_CKKC ON A.TA006=MC001 LEFT JOIN V_QTL F ON A.TA001=F.TB001 AND A.TA002=F.TB002 AND ISNULL(MF004,''****'')=F.TB006 LEFT JOIN CMSMW ON MF004=MW001";
                        //strSql += " WHERE TA011<>''y'' AND TA011<>''Y'' AND (A.TA006 LIKE ''" + textBox7.Text + "%'' OR ''" + textBox7.Text + "''='''')  AND (TD020 like ''%" + textBox6.Text + "%'' OR ''" + textBox6.Text + "''='''') AND (A.TA002 LIKE ''%" + textBox4.Text + "%'' OR ''" + textBox4.Text + "''='''') AND (INVMB.MB002 LIKE ''%" + textBox5.Text + "%'' OR INVMB.MB003 LIKE ''%" + textBox5.Text + "%'' OR ''" + textBox5.Text + "''='''') AND ISNULL(MW002,'''') LIKE ''%" + textBox9.Text.ToString() + "%'' AND (C.TY009 LIKE ''%" + textBox8.Text + "%'' OR ''" + textBox8.Text + "''='''') group by A.TA001,A.TA002,COPTD.TD020,RTRIM(A.TA006),RTRIM(A.TA033),MB002,MB003,A.TA015,D.TY004,A.TA017,RC,RTRIM(C.TY009),RTRIM(C.TY020),ISNULL(MC007,0),QTL,A.TA021,MF003,ISNULL(MF004,''****''),ISNULL(TD013,DDJQ),MW002,A.TA083 ORDER BY TA001,TA002,MF003' EXEC(@sql1)";
                        strSql = "declare @sql1 VARCHAR(8000) set @sql1 = 'SELECT SUBSTRING(A.DOC_NO, 1, CHARINDEX(''-'', A.DOC_NO, 1) - 1) AS 工单别, SUBSTRING(A.DOC_NO, CHARINDEX(''-'', A.DOC_NO, 1) + 1, LEN(A.DOC_NO) - 5) 工单号,RTRIM(ITEM.ITEM_CODE) AS 产品品号,RTRIM(A.UDF021) AS 订单号,ITEM.ITEM_NAME AS 品名,ITEM.ITEM_SPECIFICATION+'';''+RTRIM(A.UDF023)  AS 规格,OPERATION_SEQ 工序号,ISNULL(OPERATION_CODE,''****'') 工艺,ISNULL(OPERATION_NAME,'''') 工艺名称,ISNULL(A.PLAN_START_DATE,DDJQ) 预计开工日,A.REMARK 订单备注,CEILING(A.PLAN_QTY) AS 数量,ISNULL(CEILING(D.TY004),0) AS 已排产量,CEILING(ISNULL(D.TY006,0)) AS 已报工量,CEILING(A.PLAN_QTY-CEILING(ISNULL(D.TY006,0))) AS 未生产量,RTRIM(ISNULL(WORK_CENTER.WORK_CENTER_CODE,'''')) 工作中心,'''' 生产线名称,CEILING(ISNULL(MC007,0)) AS 库存量,ISNULL((SELECT CAST(ISNULL(SUM(XQL),0) AS REAL) FROM V_XQL WHERE TY003>=CONVERT(CHAR(8),GETDATE(),112) AND RTRIM(TB003)=RTRIM(ITEM.ITEM_CODE)),0) AS 总需求量,ISNULL(F.QTL,0) AS 齐套率,ISNULL(PROJECT_CODE,'''') 生产线' select @sql1 = @sql1 + ' , max(case TY003 when ''' + TY003 + ''' then CEILING(ISNULL(C.TY004,0)) else 0 end) [' + TY003 + ']' from (SELECT DISTINCT RQ001 AS TY003 FROM CMSRQ WHERE RQ001>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox1.Text + "'),112) AND RQ001<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox3.Text + "'),112)) as a set @sql1 = @sql1 + ' FROM MO A LEFT JOIN ITEM ON A.ITEM_ID=ITEM.ITEM_BUSINESS_ID LEFT JOIN V_DDJQ ON A.UDF021=DDH LEFT JOIN ITEM_ROUTING ON A.ITEM_ID=ITEM_ROUTING.ITEM_ID LEFT JOIN ITEM_ROUTING_D ON ITEM_ROUTING_D.ITEM_ROUTING_ID=ITEM_ROUTING.ITEM_ROUTING_ID LEFT JOIN OPERATION ON ITEM_ROUTING_D.OPERATION_ID=OPERATION.OPERATION_ID LEFT JOIN V_GXYPC D ON SUBSTRING(A.DOC_NO, 1, CHARINDEX(''-'', A.DOC_NO, 1) - 1)=D.TY001 AND SUBSTRING(A.DOC_NO, CHARINDEX(''-'', A.DOC_NO, 1) + 1, LEN(A.DOC_NO) - 5)=D.TY002 AND ISNULL(OPERATION_SEQ,''****'')=D.TY019 LEFT JOIN MOCTY C ON SUBSTRING(A.DOC_NO, 1, CHARINDEX(''-'', A.DOC_NO, 1) - 1)=C.TY001  AND SUBSTRING(A.DOC_NO, CHARINDEX(''-'', A.DOC_NO, 1) + 1, LEN(A.DOC_NO) - 5)=C.TY002  AND (RTRIM(D.TY019)=RTRIM(C.TY019) OR D.TY019=''****'')  LEFT JOIN CMSMX ON RTRIM(MX001)=RTRIM(C.TY009) AND RTRIM(D.TY019)=RTRIM(C.TY019)  LEFT JOIN GXSB ON ITEM.ITEM_CODE=PH  AND C.TY009=SB  LEFT JOIN V_CKKC ON ITEM.ITEM_CODE=MC001 LEFT JOIN V_QTL F ON SUBSTRING(A.DOC_NO, 1, CHARINDEX(''-'', A.DOC_NO, 1) - 1)=F.TB001 AND SUBSTRING(A.DOC_NO, CHARINDEX(''-'', A.DOC_NO, 1) + 1, LEN(A.DOC_NO) - 5)=F.TB002 AND ISNULL(OPERATION_CODE,''****'')=F.TB006 LEFT JOIN WORK_CENTER ON A.SOURCE_ID_ROid=WORK_CENTER.WORK_CENTER_ID LEFT JOIN PROJECT ON A.PROJECT_ID=PROJECT.PROJECT_ID";
                        strSql += " WHERE A.[STATUS]<>''y'' AND A.[STATUS]<>''Y'' AND A.[ApproveStatus]=''Y'' AND (ITEM.ITEM_CODE LIKE ''" + textBox7.Text + "%'' OR ''" + textBox7.Text + "''='''') AND (A.UDF021 like ''%" + textBox6.Text + "%'' OR ''" + textBox6.Text + "''='''') AND (SUBSTRING(A.DOC_NO, CHARINDEX(''-'', A.DOC_NO, 1) + 1, LEN(A.DOC_NO) - 5) LIKE ''%" + textBox4.Text + "%'' OR ''" + textBox4.Text + "''='''') AND (ITEM.ITEM_NAME LIKE ''%" + textBox5.Text + "%'' OR ITEM.ITEM_SPECIFICATION LIKE ''%" + textBox5.Text + "%'' OR ''" + textBox5.Text + "''='''') AND (C.TY009 LIKE ''%" + textBox8.Text + "%'' OR ''" + textBox8.Text + "''='''') AND A.PLAN_QTY-A.COMPLETED_QTY>0 group by A.DOC_NO,A.REMARK,RTRIM(ITEM.ITEM_CODE),RTRIM(A.UDF021),ITEM.ITEM_NAME,ITEM.ITEM_SPECIFICATION,A.PLAN_QTY,A.COMPLETED_QTY,D.TY004,D.TY006,RC,RTRIM(C.TY009),RTRIM(C.TY020),ISNULL(MC007,0),QTL,WORK_CENTER.WORK_CENTER_CODE,ISNULL(A.PLAN_START_DATE,DDJQ),A.UDF023,OPERATION_SEQ,ISNULL(OPERATION_CODE,''****''),ISNULL(OPERATION_NAME,''''),ISNULL(PROJECT_CODE,'''') ORDER BY A.DOC_NO,OPERATION_SEQ' EXEC(@sql1)";
                    }

                    if (checkBox2.CheckState == CheckState.Checked)
                    {
                        //strSql = "declare @sql1 VARCHAR(8000) set @sql1 = 'SELECT A.TA001 AS 工单别,A.TA002 AS 工单号,RTRIM(A.TA006) AS 产品品号,RTRIM(TD020) AS 订单号,MB002 AS 品名,MB003  AS 规格,MF003 AS 工序号,ISNULL(MF004,''****'') AS 工艺,MW002 AS 工艺名称,ISNULL(TD013,DDJQ) 订单交期,TD020 订单备注,CEILING(A.TA015) AS 数量,ISNULL(CEILING(D.TY004),0) AS 已排产量,CEILING(A.TA017) AS 已入库量,CEILING(A.TA015-A.TA017) AS 未生产量,RTRIM(A.TA021) 工作中心,'''' 生产线名称,CEILING(RC) 班产量,CEILING(ISNULL(MC007,0)) AS 库存量,ISNULL((SELECT ISNULL(SUM(XQL),0) FROM V_XQL WHERE TY003>=CONVERT(CHAR(8),GETDATE(),112) AND RTRIM(TB003)=RTRIM(A.TA006)),0) AS 总需求量,ISNULL(F.QTL,0) AS 齐套率,RTRIM(A.TA083) 生产线' select @sql1 = @sql1 + ' , max(case TY003 when ''' + TY003 + ''' then CEILING(ISNULL(C.TY004,0)) else 0 end) [' + TY003 + ']' from (SELECT DISTINCT RQ001 AS TY003 FROM CMSRQ WHERE RQ001>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox1.Text + "'),112) AND RQ001<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox3.Text + "'),112)) as a set @sql1 = @sql1 + ' FROM MOCTA A LEFT JOIN COPTD ON A.TA026=COPTD.TD001 AND A.TA027=COPTD.TD002 AND A.TA028=COPTD.TD003 LEFT JOIN INVMB ON A.TA006=MB001 LEFT JOIN V_DDJQ ON TA027=DDH  LEFT JOIN BOMMF ON MB010=MF001 AND MB011=MF002 LEFT JOIN V_GXYPC D ON A.TA001=D.TY001 AND A.TA002=D.TY002 AND ISNULL(MF004,''****'')=D.TY019  LEFT JOIN MOCTY C ON A.TA001=C.TY001  AND A.TA002=C.TY002  AND RTRIM(D.TY019)=RTRIM(C.TY019)  LEFT JOIN CMSMX ON RTRIM(MX001)=RTRIM(C.TY009) AND RTRIM(D.TY019)=RTRIM(C.TY019)  LEFT JOIN GXSB ON A.TA006=PH  AND C.TY009=SB  LEFT JOIN V_CKKC ON A.TA006=MC001 LEFT JOIN V_QTL F ON A.TA001=F.TB001 AND A.TA002=F.TB002 AND ISNULL(MF004,''****'')=F.TB006  LEFT JOIN CMSMW ON MF004=MW001";
                        //strSql += " WHERE ISNULL(CEILING(D.TY004),0)>0 AND TA011<>''y'' AND TA011<>''Y'' AND (A.TA006 LIKE ''" + textBox7.Text + "%'' OR ''" + textBox7.Text + "''='''') AND (TD020 like ''%" + textBox6.Text + "%'' OR ''" + textBox6.Text + "''='''') AND (A.TA002 LIKE ''%" + textBox4.Text + "%'' OR ''" + textBox4.Text + "''='''') AND (INVMB.MB002 LIKE ''%" + textBox5.Text + "%'' OR INVMB.MB003 LIKE ''%" + textBox5.Text + "%'' OR ''" + textBox5.Text + "''='''') AND ISNULL(MW002,'''') LIKE ''%" + textBox9.Text.ToString() + "%'' AND (C.TY009 LIKE ''%" + textBox8.Text + "%'' OR ''" + textBox8.Text + "''='''') group by A.TA001,A.TA002,COPTD.TD020,RTRIM(A.TA006),RTRIM(A.TA033),MB002,MB003,A.TA015,D.TY004,A.TA017,RC,RTRIM(C.TY009),RTRIM(C.TY020),ISNULL(MC007,0),QTL,A.TA021,MF003,ISNULL(MF004,''****''),ISNULL(TD013,DDJQ),MW002,A.TA083 ORDER BY TA001,TA002,MF003' EXEC(@sql1)";
                        strSql = "declare @sql1 VARCHAR(8000) set @sql1 = 'SELECT SUBSTRING(A.DOC_NO, 1, CHARINDEX(''-'', A.DOC_NO, 1) - 1) AS 工单别, SUBSTRING(A.DOC_NO, CHARINDEX(''-'', A.DOC_NO, 1) + 1, LEN(A.DOC_NO) - 5) 工单号,RTRIM(ITEM.ITEM_CODE) AS 产品品号,RTRIM(A.UDF021) AS 订单号,ITEM.ITEM_NAME AS 品名,ITEM.ITEM_SPECIFICATION+'';''+RTRIM(A.UDF023)  AS 规格,OPERATION_SEQ 工序号,ISNULL(OPERATION_CODE,''****'') 工艺,ISNULL(OPERATION_NAME,'''') 工艺名称,ISNULL(A.PLAN_START_DATE,DDJQ) 预计开工日,A.REMARK 订单备注,CEILING(A.PLAN_QTY) AS 数量,ISNULL(CEILING(D.TY004),0) AS 已排产量,CEILING(ISNULL(D.TY006,0)) AS 已报工量,CEILING(A.PLAN_QTY-CEILING(ISNULL(D.TY006,0))) AS 未生产量,RTRIM(ISNULL(WORK_CENTER.WORK_CENTER_CODE,'''')) 工作中心,'''' 生产线名称,CEILING(ISNULL(MC007,0)) AS 库存量,ISNULL((SELECT CAST(ISNULL(SUM(XQL),0) AS REAL) FROM V_XQL WHERE TY003>=CONVERT(CHAR(8),GETDATE(),112) AND RTRIM(TB003)=RTRIM(ITEM.ITEM_CODE)),0) AS 总需求量,ISNULL(F.QTL,0) AS 齐套率,ISNULL(PROJECT_CODE,'''') 生产线' select @sql1 = @sql1 + ' , max(case TY003 when ''' + TY003 + ''' then CEILING(ISNULL(C.TY004,0)) else 0 end) [' + TY003 + ']' from (SELECT DISTINCT RQ001 AS TY003 FROM CMSRQ WHERE RQ001>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox1.Text + "'),112) AND RQ001<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox3.Text + "'),112)) as a set @sql1 = @sql1 + ' FROM MO A LEFT JOIN ITEM ON A.ITEM_ID=ITEM.ITEM_BUSINESS_ID LEFT JOIN V_DDJQ ON A.UDF021=DDH LEFT JOIN ITEM_ROUTING ON A.ITEM_ID=ITEM_ROUTING.ITEM_ID LEFT JOIN ITEM_ROUTING_D ON ITEM_ROUTING_D.ITEM_ROUTING_ID=ITEM_ROUTING.ITEM_ROUTING_ID LEFT JOIN OPERATION ON ITEM_ROUTING_D.OPERATION_ID=OPERATION.OPERATION_ID LEFT JOIN V_GXYPC D ON SUBSTRING(A.DOC_NO, 1, CHARINDEX(''-'', A.DOC_NO, 1) - 1)=D.TY001 AND SUBSTRING(A.DOC_NO, CHARINDEX(''-'', A.DOC_NO, 1) + 1, LEN(A.DOC_NO) - 5)=D.TY002 AND ISNULL(OPERATION_SEQ,''****'')=D.TY019 LEFT JOIN MOCTY C ON SUBSTRING(A.DOC_NO, 1, CHARINDEX(''-'', A.DOC_NO, 1) - 1)=C.TY001  AND SUBSTRING(A.DOC_NO, CHARINDEX(''-'', A.DOC_NO, 1) + 1, LEN(A.DOC_NO) - 5)=C.TY002  AND (RTRIM(D.TY019)=RTRIM(C.TY019) OR D.TY019=''****'')  LEFT JOIN CMSMX ON RTRIM(MX001)=RTRIM(C.TY009) AND RTRIM(D.TY019)=RTRIM(C.TY019)  LEFT JOIN GXSB ON ITEM.ITEM_CODE=PH  AND C.TY009=SB  LEFT JOIN V_CKKC ON ITEM.ITEM_CODE=MC001 LEFT JOIN V_QTL F ON SUBSTRING(A.DOC_NO, 1, CHARINDEX(''-'', A.DOC_NO, 1) - 1)=F.TB001 AND SUBSTRING(A.DOC_NO, CHARINDEX(''-'', A.DOC_NO, 1) + 1, LEN(A.DOC_NO) - 5)=F.TB002 AND ISNULL(OPERATION_CODE,''****'')=F.TB006 LEFT JOIN WORK_CENTER ON A.SOURCE_ID_ROid=WORK_CENTER.WORK_CENTER_ID LEFT JOIN PROJECT ON A.PROJECT_ID=PROJECT.PROJECT_ID";
                        strSql += " WHERE ISNULL(CEILING(D.TY004),0)>0 AND A.[STATUS]<>''y'' AND A.[STATUS]<>''Y'' AND A.[ApproveStatus]=''Y'' AND (ITEM.ITEM_CODE LIKE ''" + textBox7.Text + "%'' OR ''" + textBox7.Text + "''='''') AND (A.UDF021 like ''%" + textBox6.Text + "%'' OR ''" + textBox6.Text + "''='''') AND (SUBSTRING(A.DOC_NO, CHARINDEX(''-'', A.DOC_NO, 1) + 1, LEN(A.DOC_NO) - 5) LIKE ''%" + textBox4.Text + "%'' OR ''" + textBox4.Text + "''='''') AND (ITEM.ITEM_NAME LIKE ''%" + textBox5.Text + "%'' OR ITEM.ITEM_SPECIFICATION LIKE ''%" + textBox5.Text + "%'' OR ''" + textBox5.Text + "''='''') AND (C.TY009 LIKE ''%" + textBox8.Text + "%'' OR ''" + textBox8.Text + "''='''') AND A.PLAN_QTY-A.COMPLETED_QTY>0 group by A.DOC_NO,A.REMARK,RTRIM(ITEM.ITEM_CODE),RTRIM(A.UDF021),ITEM.ITEM_NAME,ITEM.ITEM_SPECIFICATION,A.PLAN_QTY,A.COMPLETED_QTY,D.TY004,D.TY006,RC,RTRIM(C.TY009),RTRIM(C.TY020),ISNULL(MC007,0),QTL,WORK_CENTER.WORK_CENTER_CODE,ISNULL(A.PLAN_START_DATE,DDJQ),A.UDF023,OPERATION_SEQ,ISNULL(OPERATION_CODE,''****''),ISNULL(OPERATION_NAME,''''),ISNULL(PROJECT_CODE,'''') ORDER BY A.DOC_NO,OPERATION_SEQ' EXEC(@sql1)";
                    }
                }
               // textBox6.Text = strSql;
                try
                {
                    DataTable dt;
                    this.gridControl1.DataSource = "";
                    this.gridControl1.Refresh();

                    dt = db.GetDataSet1(strSql, "MOCTA").Tables["MOCTA"];
                    DataView view = new DataView();
                    view.Table = dt;
                    view.RowFilter = "工作中心 = '" + textBox2.Text + "'";//itemType是A中的一个字段
                    //dt.Rows.Add();
                    this.gridControl1.DataSource = view.ToTable();

                    gridView1.OptionsView.EnableAppearanceEvenRow = true;
                    gridView1.OptionsView.EnableAppearanceOddRow = false;

                    //this.gridView1.Columns["工单别"].Visible = false;
                    //this.gridView1.Columns["工单号"].Visible = true;
                    //this.gridView1.Columns["工单号"].Width = 80;
                    //this.gridView1.Columns["产品品号"].Visible = true;
                    //this.gridView1.Columns["产品品号"].Width = 80;
                    //this.gridView1.Columns["订单号"].Width = 50;
                    //this.gridView1.Columns["工艺"].Visible = false;
                    //this.gridView1.Columns["已入库量"].Visible = true;
                    //this.gridView1.Columns["已入库量"].Width = 30;
                    //this.gridView1.Columns["产品品号"].Width = 50;
                    //this.gridView1.Columns["生产线名称"].Visible = false;
                    //this.gridView1.Columns["班产量"].Visible = false;
                    //this.gridView1.Columns["品名"].ReadOnly = true;
                    //this.gridView1.Columns["品名"].Frozen = true;
                    //this.gridView1.Columns["品名"].Visible = true;
                    //this.gridView1.Columns["品名"].Width = 50;
                    //this.gridView1.Columns["规格"].ReadOnly = true;
                    //this.gridView1.Columns["规格"].Width = 100;
                    //this.gridView1.Columns["规格"].Frozen = true;
                    //this.gridView1.Columns["工序号"].ReadOnly = true;
                    //this.gridView1.Columns["工序号"].Visible = false;
                    //this.gridView1.Columns["工序号"].Frozen = true;
                    //this.gridView1.Columns["工艺名称"].ReadOnly = true;
                    //this.gridView1.Columns["工艺名称"].Visible = true;
                    //this.gridView1.Columns["工艺名称"].Frozen = true;
                    //this.gridView1.Columns["工艺名称"].Width = 50;
                    //this.gridView1.Columns["订单备注"].Visible = false;
                    //this.gridView1.Columns["数量"].ReadOnly = true;
                    //this.gridView1.Columns["数量"].Frozen = true;
                    //this.gridView1.Columns["数量"].Width = 50;
                    //this.gridView1.Columns["已排产量"].ReadOnly = true;
                    //this.gridView1.Columns["已排产量"].Frozen = true;
                    //this.gridView1.Columns["已排产量"].Width = 50;
                    //this.gridView1.Columns["总需求量"].Width = 50;
                    //this.gridView1.Columns["工作中心"].Frozen = true;
                    //this.gridView1.Columns["工作中心"].Visible = false;
                    //this.gridView1.Columns["生产线名称"].ReadOnly = true;
                    //this.gridView1.Columns["生产线名称"].Frozen = true;
                    //this.gridView1.Columns["生产线"].Frozen = true;
                    //this.gridView1.Columns["生产线"].Visible = true;
                    //this.gridView1.Columns["生产线"].Width = 30;
                    //this.gridView1.Columns["库存量"].Visible = false;
                    //this.gridView1.Columns["齐套率"].DefaultCellStyle.Format = "P2";
                    //this.gridView1.Columns["齐套率"].Width = 50;
                    //    foreach (DataGridViewColumn dr in this.gridView1.Columns)
                    //    {
                    //        if (dr.Index > this.gridView1.Columns["生产线"].Index)
                    //        {
                    //            dr.Width = 60;
                    //        }

                    //    }
                    //    //this.dataGridView1.DefaultCellStyle.BackColor = Color.Yellow;
                    //    this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
                    //    this.dataGridView1.AllowUserToAddRows = true;

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    //throw ex;
                }

                //    int index = this.dataGridView1.Rows.Count - 2;
                //    //this.dataGridView1.Rows[index].Cells[this.dataGridView1.Columns["齐套率"].Index].Value = "合计";
                //    this.dataGridView1.Rows[index].DefaultCellStyle.BackColor = Color.Yellow;
                //for (int i = this.gridView1.Columns["生产线"].ColumnHandle + 1; i <= gridView1.Columns.Count - 1; i++)
                //{
                //double sum2 = 0;
                //for (int l = 0; l < this.dataGridView1.Rows.Count - 2; l++)
                //{
                //    sum2 += double.Parse(this.dataGridView1.Rows[l].Cells[i].Value == null ? "0" : this.dataGridView1.Rows[l].Cells[i].Value.ToString());
                //}
                //this.dataGridView1.Rows[index].Cells[i].Value = sum2;

                //gridView1.Columns[i].SummaryItem.DisplayFormat = "{0:0.##}";
                //gridView1.Columns[i].SummaryItem.FieldName = gridView1.Columns[i].FieldName;
                //gridView1.Columns[i].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;//求和
                //}

                foreach (DevExpress.XtraGrid.Columns.GridColumn dr in this.gridView1.Columns)
                {
                    if (dr.ColumnHandle > this.gridView1.Columns["生产线"].ColumnHandle)
                    {
                        dr.Width = 60;
                        String djrq = dr.Caption.ToString().Trim();
                        DateTime dta = Convert.ToDateTime(djrq.Substring(0, 4) + "-" + djrq.Substring(4, 2) + "-" + djrq.Substring(6, 2));
                        int a = Convert.ToInt32(dta.DayOfWeek);
                        if (a == 3 || a == 6)
                        {
                            dr.AppearanceHeader.BackColor = Color.Blue;
                        }
                    }

                }
            }
            catch
            {

            }

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
           // button1.PerformClick();

        }

        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            //this.dataGridView1.Controls.Clear();//移除所有控件 
            //if (e.ColumnIndex.Equals(this.dataGridView1.Columns["生产线"].Index))
            ////if (e.ColumnIndex == 13)
            ////判断单元格是否是"Company"列？    
            //{
            //    System.Windows.Forms.Button btn = new System.Windows.Forms.Button();//创建Buttonbtn    
            //    btn.Text = "...";//设置button文字    
            //    btn.Font = new System.Drawing.Font("Arial", 5);//设置文字格式    
            //    btn.Visible = true;//设置控件允许显示    

            //    btn.Width = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,
            //                    e.RowIndex, true).Height;//获取单元格高并设置为btn的宽    
            //    btn.Height = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,
            //                    e.RowIndex, true).Height;//获取单元格高并设置为btn的高    

            //    btn.Click += new EventHandler(btn_Click);//为btn添加单击事件    

            //    this.dataGridView1.Controls.Add(btn);//dataGridView1中添加控件btn    

            //    btn.Location = new System.Drawing.Point(((this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Right) -
            //            (btn.Width)), this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Y);//设置btn显示位置    
            //}

            //if (e.ColumnIndex.Equals(this.dataGridView1.Columns["工作中心"].Index))
            ////if (e.ColumnIndex == 13)
            ////判断单元格是否是"Company"列？    
            //{
            //    System.Windows.Forms.Button btn = new System.Windows.Forms.Button();//创建Buttonbtn    
            //    btn.Text = "...";//设置button文字    
            //    btn.Font = new System.Drawing.Font("Arial", 5);//设置文字格式    
            //    btn.Visible = true;//设置控件允许显示    

            //    btn.Width = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,
            //                    e.RowIndex, true).Height;//获取单元格高并设置为btn的宽    
            //    btn.Height = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,
            //                    e.RowIndex, true).Height;//获取单元格高并设置为btn的高    

            //    btn.Click += new EventHandler(btnA_Click);//为btn添加单击事件    

            //    this.dataGridView1.Controls.Add(btn);//dataGridView1中添加控件btn    

            //    btn.Location = new System.Drawing.Point(((this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Right) -
            //            (btn.Width)), this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Y);//设置btn显示位置    
            //}

        }

        void btn_Click(object sender, EventArgs e)
        {
            string x1 = gridView1.GetFocusedRowCellValue("工作中心").ToString();
            SCGY xx = new SCGY(x1);
            f = new SCKC();
            f.DataSelected += new EventHandler(f_DataSelected);
            f.cdgzzx += new SCKC.Cdgzzx(textBox2_TextChanged);
            f.Show(this);
        }

        void btnA_Click(object sender, EventArgs e)
        {
            //string x1 = dataGridView1["工作中心", dataGridView1.CurrentCell.RowIndex].Value.ToString();
            //SCGY xx = new SCGY(x1);
            k = new GZZXKC();
            k.DataSelected += new EventHandler(k_DataSelected);
            //f.cdgzzx += new SCKC.Cdgzzx(textBox2_TextChanged);
            k.Show(this);
        }

        void f_DataSelected(object sender, EventArgs e)
        {
            this.gridView1.SetFocusedRowCellValue("生产线",f.UserId);
            this.gridView1.SetFocusedRowCellValue("生产线名称", f.UserName);
        }

        void k_DataSelected(object sender, EventArgs e)
        {

            //this.dataGridView1.CurrentCell.Value = k.UserId;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {


        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //if (PropertyClass.jiaose == "计划员" || PropertyClass.jiaose == "计划主管" || PropertyClass.jiaose == "超级管理员" || PropertyClass.jiaose == "车间统计员")
            //{
            //    string a = PropertyClass.gzzx.ToString();
            //    string b = dataGridView1["工作中心", dataGridView1.CurrentCell.RowIndex].Value.ToString();
            //    if (a.IndexOf(b) <= -1 || b =="")
            //    {
            //        MessageBox.Show("对不起,您没有对工作中心" + b + "进行排产的权限", "软件提示");
            //        return;
            //    }
            //if (this.dataGridView1.CurrentCell.Value.ToString() != "")
            //{
            //    if (e.ColumnIndex.Equals(this.dataGridView1.Columns["生产线"].Index))
            //    {
            //        string strSql = null;
            //        strSql = "SELECT MX001,MX003 FROM CMSMX  WHERE MX001 ='" + this.dataGridView1.CurrentCell.Value.ToString() + "' AND MX002='" + dataGridView1["工作中心", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "'";
            //        try
            //        {
            //            sdr1 = db.GetDataReader1(strSql);

            //            while (sdr1.Read())
            //            {
            //                this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["生产线名称"].Index].Value = sdr1["MX003"].ToString();

            //            }

            //            if (sdr1.HasRows == false)
            //            {
            //                MessageBox.Show("该生产线不存在！", "软件提示");
            //                this.dataGridView1.CurrentCell.Value = "";
            //            }

            //        }

            //        catch (Exception ex)
            //        {
            //            MessageBox.Show(ex.Message, "软件提示");
            //            throw ex;
            //        }
            //        finally
            //        {
            //            sdr1.Close();
            //        }

            //    }
            //    if (e.RowIndex > -1 && e.ColumnIndex > this.dataGridView1.Columns["生产线"].Index)
            //    {
            //        //MessageBox.Show(dqsl.ToString(), "提示");
            //        string strSql = null;
            //        double ypcl = 0;
            //        double wpcl = 0;
            //        //if (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["已排产量"].Index].Value == DBNull.Value)
            //        //{
            //        //    ypcl = Convert.ToDouble(this.dataGridView1.CurrentCell.Value);
            //        //    wpcl = Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["数量"].Index].Value);
            //        //}
            //        //else
            //        //{
            //        //    ypcl = Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["已排产量"].Index].Value) + Convert.ToDouble(this.dataGridView1.CurrentCell.Value)-dqsl;
            //        //    wpcl = Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["数量"].Index].Value) - Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["已排产量"].Index].Value)+dqsl;
            //        //   // MessageBox.Show("保存成功！", "软体提示");
            //        //}

            //        strSql = "SELECT ISNULL(SUM(TY004),0) TY004 FROM MOCTY  WHERE TY001 ='" + dataGridView1["工单别", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY002='" + dataGridView1["工单号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY019='" + dataGridView1["工序号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "'";
            //        try
            //        {
            //            sdr1 = db.GetDataReader1(strSql);

            //            while (sdr1.Read())
            //            {
            //                ypcl = Convert.ToDouble(sdr1["TY004"]);

            //            }

            //            if (sdr1.HasRows == false)
            //            {
            //              ypcl=0; 
            //            }

            //        }
            //        catch (Exception ex)
            //        {
            //            MessageBox.Show(ex.Message, "软件提示");
            //            throw ex;
            //        }
            //        finally
            //        {
            //            sdr1.Close();
            //        }

            //        strSql = "SELECT ISNULL(SUM(TY004),0) TY004 FROM MOCTY  WHERE TY001 ='" + dataGridView1["工单别", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY002='" + dataGridView1["工单号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY019='" + dataGridView1["工序号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY003='" + this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].Name.ToString() + "' AND TY009='" +  dataGridView1["生产线", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "'";
            //        try
            //        {
            //            sdr1 = db.GetDataReader1(strSql);

            //            while (sdr1.Read())
            //            {
            //                dqsl = Convert.ToDouble(sdr1["TY004"]);

            //            }

            //            if (sdr1.HasRows == false)
            //            {
            //                dqsl = 0;  
            //            }

            //        }
            //        catch (Exception ex)
            //        {
            //            MessageBox.Show(ex.Message, "软件提示");
            //            throw ex;
            //        }
            //        finally
            //        {
            //            sdr1.Close();
            //        }
            //        wpcl = Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["数量"].Index].Value) - ypcl;
            //        if (ypcl+Convert.ToDouble(this.dataGridView1.CurrentCell.Value.ToString())-dqsl > Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["数量"].Index].Value))
            //        {
            //            MessageBox.Show("最多只能排" + wpcl.ToString(), "提示");
            //            this.dataGridView1.CurrentCell.Value = wpcl;
            //            //MessageBox.Show(row.ToString(), "提示");
            //            //MessageBox.Show(column.ToString(), "提示");
            //            //this.dataGridView1.CurrentCell = this.dataGridView1.Rows[row-1].Cells[column];
            //            if (dataGridView1["生产线", dataGridView1.CurrentCell.RowIndex].Value == DBNull.Value)
            //            {
            //                strSql = "UPDATE MOCTY SET TY004=" + this.dataGridView1.CurrentCell.Value.ToString() + " WHERE TY001='" + dataGridView1["工单别", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY002='" + dataGridView1["工单号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY003='" + this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].Name.ToString() + "' AND TY009='' AND TY019='" + dataGridView1["工序号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "'";
            //            }
            //            else
            //            {
            //                strSql = "UPDATE MOCTY SET TY004=" + this.dataGridView1.CurrentCell.Value.ToString() + " WHERE TY001='" + dataGridView1["工单别", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY002='" + dataGridView1["工单号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY003='" + this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].Name.ToString() + "' AND TY009='" + dataGridView1["生产线", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY019='" + dataGridView1["工艺", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "'";
            //            }
            //           // MessageBox.Show(strSql, "提示");
            //            try
            //            {
            //                if (db.ExecDataBySql1(strSql) > 0)
            //                {
            //                    //MessageBox.Show("删除成功！", "软体提示");
            //                    this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["已排产量"].Index].Value = this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["数量"].Index].Value;

            //                    string strSql1 = "INSERT INTO DATELOG SELECT '" + dataGridView1["工单别", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','" + dataGridView1["工单号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','" + this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].Name.ToString() + "','" + dataGridView1["生产线", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','" + dataGridView1["工序号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','更新'," + dataGridView1.CurrentCell.Value.ToString() + ",CONVERT(CHAR(20),GETDATE(),120),'" + PropertyClass.OperatorName + "'";
            //                    try
            //                    {

            //                        if (db.ExecDataBySql1(strSql1) > 0)
            //                        {

            //                        }
            //                        else
            //                        {

            //                        }
            //                    }
            //                    catch (Exception ex)
            //                    {
            //                        MessageBox.Show(ex.Message, "软件提示");
            //                        throw ex;
            //                    }
                            
            //                }
            //                else
            //                {
            //                    //MessageBox.Show("保存失败！", "软体提示");
            //                    //return;
            //                }
            //            }

            //            catch (Exception ex)
            //            {
            //                MessageBox.Show(ex.Message, "软件提示");
            //                throw ex;
            //            }

            //            strSql = "INSERT INTO MOCTY(COMPANY,CREATOR,CREATE_DATE,FLAG,TY001,TY002,TY003,TY004,TY005,TY006,TY007,TY008,TY009,TY018,TY019) SELECT '','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','" + dataGridView1["工单别", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','" + dataGridView1["工单号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','" + this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].Name.ToString() + "'," + wpcl + ",0,0,0,'N',";
            //            strSql += "'" + dataGridView1["生产线", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','','" + dataGridView1["工序号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' WHERE NOT EXISTS(SELECT * FROM MOCTY WHERE TY001='" + dataGridView1["工单别", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY002='" + dataGridView1["工单号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY003='" + this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].Name.ToString() + "' AND TY009='" + dataGridView1["生产线", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY019='" + dataGridView1["工序号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "')";
                      
            //            try
            //            {
            //                if (db.ExecDataBySql1(strSql) > 0)
            //                {
            //                    this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["已排产量"].Index].Value = this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["数量"].Index].Value;

            //                    string strSql1 = "INSERT INTO DATELOG SELECT '" + dataGridView1["工单别", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','" + dataGridView1["工单号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','" + this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].Name.ToString() + "','" + dataGridView1["生产线", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','" + dataGridView1["工序号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','新增'," + dataGridView1.CurrentCell.Value.ToString() + ",CONVERT(CHAR(20),GETDATE(),120),'" + PropertyClass.OperatorName + "'";
            //                    try
            //                    {

            //                        if (db.ExecDataBySql1(strSql1) > 0)
            //                        {

            //                        }
            //                        else
            //                        {

            //                        }
            //                    }
            //                    catch (Exception ex)
            //                    {
            //                        MessageBox.Show(ex.Message, "软件提示");
            //                        throw ex;
            //                    }
            //                }
            //                else
            //                {
                                
            //                }
            //            }
            //            catch (Exception ex)
            //            {
            //                MessageBox.Show(ex.Message, "软件提示");
            //                throw ex;
            //            }


            //            return;

            //        }

            //        if (dqsl >= 0)
            //        {

            //            if (dataGridView1["生产线", dataGridView1.CurrentCell.RowIndex].Value == DBNull.Value)
            //            {
            //                strSql = "UPDATE MOCTY SET TY004=" + this.dataGridView1.CurrentCell.Value.ToString() + " WHERE TY001='" + dataGridView1["工单别", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY002='" + dataGridView1["工单号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY003='" + this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].Name.ToString() + "' AND TY009='' AND TY019='" + dataGridView1["工序号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "'";
            //            }
            //            else
            //            {
            //                strSql = "UPDATE MOCTY SET TY004=" + this.dataGridView1.CurrentCell.Value.ToString() + " WHERE TY001='" + dataGridView1["工单别", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY002='" + dataGridView1["工单号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY003='" + this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].Name.ToString() + "' AND TY009='" + dataGridView1["生产线", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY019='" + dataGridView1["工序号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "'";
            //            }
            //            //MessageBox.Show(strSql,"提示");
            //            try
            //            {
            //                if (db.ExecDataBySql1(strSql) > 0)
            //                {
            //                    if (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["已排产量"].Index].Value == DBNull.Value)
            //                    {
            //                        this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["已排产量"].Index].Value = Convert.ToDouble(this.dataGridView1.CurrentCell.Value);
            //                    }
            //                    else
            //                    {
            //                        this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["已排产量"].Index].Value = ypcl + Convert.ToDouble(this.dataGridView1.CurrentCell.Value)-dqsl;
            //                        //MessageBox.Show("保存成功！", "软体提示");
            //                    }
            //                    string strSql1 = "INSERT INTO DATELOG SELECT '" + dataGridView1["工单别", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','" + dataGridView1["工单号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','" + this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].Name.ToString() + "','" + dataGridView1["生产线", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','" + dataGridView1["工序号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','更新'," + dataGridView1.CurrentCell.Value.ToString() + ",CONVERT(CHAR(20),GETDATE(),120),'" + PropertyClass.OperatorName + "'";
            //                    try
            //                    {

            //                        if (db.ExecDataBySql1(strSql1) > 0)
            //                        {

            //                        }
            //                        else
            //                        {

            //                        }
            //                    }
            //                    catch (Exception ex)
            //                    {
            //                        MessageBox.Show(ex.Message, "软件提示");
            //                        throw ex;
            //                    }
            //                }
            //                else
            //                {
            //                    //MessageBox.Show("保存失败！", "软体提示");
            //                    //return;
            //                }
            //            }

            //            catch (Exception ex)
            //            {
            //                MessageBox.Show(ex.Message, "软件提示");
            //                throw ex;
            //            }
            //        }

            //        strSql = "INSERT INTO MOCTY(COMPANY,CREATOR,CREATE_DATE,FLAG,TY001,TY002,TY003,TY004,TY005,TY006,TY007,TY008,TY009,TY018,TY019) SELECT '','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','" + dataGridView1["工单别", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','" + dataGridView1["工单号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','" + this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].Name.ToString() + "'," + this.dataGridView1.CurrentCell.Value.ToString() + ",0,0,0,'N',";
            //        strSql += "'" + dataGridView1["生产线", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','','" + dataGridView1["工序号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "'  WHERE NOT EXISTS(SELECT * FROM MOCTY WHERE TY001='" + dataGridView1["工单别", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY002='" + dataGridView1["工单号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY003='" + this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].Name.ToString() + "' AND TY009='" + dataGridView1["生产线", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY019='" + dataGridView1["工序号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "')";
            //        try
            //        {
            //            if (db.ExecDataBySql1(strSql) > 0)
            //            {
            //                if (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["已排产量"].Index].Value == DBNull.Value)
            //                {
            //                    this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["已排产量"].Index].Value = Convert.ToDouble(this.dataGridView1.CurrentCell.Value);
                                
            //                }
            //                else
            //                {
            //                    this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["已排产量"].Index].Value = Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["已排产量"].Index].Value) + Convert.ToDouble(this.dataGridView1.CurrentCell.Value);
            //                    //MessageBox.Show("保存成功！", "软体提示");
            //                }

            //                string strSql1 = "INSERT INTO DATELOG SELECT '" + dataGridView1["工单别", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','" + dataGridView1["工单号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','" + this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].Name.ToString() + "','" + dataGridView1["生产线", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','" + dataGridView1["工序号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','新增'," + dataGridView1.CurrentCell.Value.ToString() + ",CONVERT(CHAR(20),GETDATE(),120),'" + PropertyClass.OperatorName + "'";
            //                    try
            //                    {

            //                        if (db.ExecDataBySql1(strSql1) > 0)
            //                        {

            //                        }
            //                        else
            //                        {

            //                        }
            //                    }
            //                    catch (Exception ex)
            //                    {
            //                        MessageBox.Show(ex.Message, "软件提示");
            //                        throw ex;
            //                    }
            //            }
            //            else
            //            {
            //                //MessageBox.Show("保存失败！", "软体提示");
            //                //return;
            //            }
            //        }
            //        catch (Exception ex)
            //        {
            //            MessageBox.Show(ex.Message, "软件提示");
            //            throw ex;
            //        }
            //        //finally
            //        //{
            //        //    sdr1.Close();
            //        //}
                   

            //    }
            //}
            //}

            //if (PropertyClass.jiaose != "计划员" && PropertyClass.jiaose != "计划主管" && PropertyClass.jiaose != "超级管理员" && PropertyClass.jiaose != "车间统计员")
            //{
            //     MessageBox.Show("对不起,您没有修改权限", "软件提示");
            //    return;
            //}
        }

        public SCKC.Cdgzzx SCKC_Load { get; set; }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            //try
            //{
            //    if (e.RowIndex > -1 && e.ColumnIndex > this.dataGridView1.Columns["生产线"].Index)
            //    {
            //        if (e.Value == DBNull.Value)
            //        {
            //            e.Value = "";
            //        }
            //        else
            //        {
            //            if (Convert.ToDecimal(e.Value) == Convert.ToDecimal("0.00"))
            //            {
            //                e.Value = "";
            //            }
            //        }
            //    }
            //    if (Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["总需求量"].Index].Value) > Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["库存量"].Index].Value) + Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["已排产量"].Index].Value))
            //    {

            //        for (int i = 0; i <= this.dataGridView1.Columns["生产线"].Index; i++)
            //        {
            //            this.dataGridView1.Rows[e.RowIndex].Cells[i].Style.BackColor = Color.Red;
            //        }
            //    }
            //    if (Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["齐套率"].Index].Value) == 1.00)
            //    {

            //        for (int i = this.dataGridView1.Columns["生产线"].Index + 1; i <= this.dataGridView1.Columns.Count - 1; i++)
            //        {
            //            this.dataGridView1.Rows[e.RowIndex].Cells[i].Style.BackColor = Color.Green;
            //        }
            //    }
            //}
            //catch
            //{
            
            //}
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            //button1.PerformClick();
        }

        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            //if (e.RowIndex > -1 && e.ColumnIndex > 15)
            //{
            //    double ypcl = 0;
            //    double wpcl = 0;
                
            //    if (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["已排产量"].Index].Value == DBNull.Value)
            //    {
            //        ypcl = Convert.ToDouble(this.dataGridView1.CurrentCell.Value);
            //        wpcl = Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["数量"].Index].Value);
            //    }
            //    else
            //    {
            //        ypcl = Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["已排产量"].Index].Value) + Convert.ToDouble(this.dataGridView1.CurrentCell.Value);
            //        wpcl = Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["数量"].Index].Value) - Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["已排产量"].Index].Value);
            //        //MessageBox.Show("保存成功！", "软体提示");
            //    }
            //    MessageBox.Show( ypcl.ToString(), "提示");
            //    if (ypcl > Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["数量"].Index].Value))
            //    {
            //        MessageBox.Show("最多只能排" + wpcl.ToString(), "提示");
            //        this.dataGridView1.CurrentCell.Value = 0;
            //        e.Cancel = true;
            //        return;
            //    }
            //}
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //try
            //{
            //    if (e.RowIndex > -1 && e.ColumnIndex > this.dataGridView1.Columns["生产线"].Index)
            //    {
            //        dqsl = Convert.ToDouble(this.dataGridView1.CurrentCell.Value);
            //        row = this.dataGridView1.CurrentCell.RowIndex;
            //        column = this.dataGridView1.CurrentCell.ColumnIndex;
            //    }
            //}
            //catch
            //{
            //}
        }

    



        private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
        {
            //if (this.dataGridView1.CurrentCell.RowIndex > -1 && this.dataGridView1.CurrentCell.ColumnIndex > this.dataGridView1.Columns["生产线"].Index)
            //{
            //    dqsl = Convert.ToDouble(this.dataGridView1.CurrentCell.Value);
             
            //    row = this.dataGridView1.CurrentCell.RowIndex;
            //    column = this.dataGridView1.CurrentCell.ColumnIndex;
            //}
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //if (e.RowIndex > -1 && e.ColumnIndex > this.dataGridView1.Columns["生产线"].Index)
            //{
            //    double dpsl = 0;
            //    double dbcl = 0;

            //    if (this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[this.dataGridView1.Columns["已排产量"].Index].Value == DBNull.Value)
            //    {
            //        dpsl = Convert.ToDouble(this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[this.dataGridView1.Columns["数量"].Index].Value);

            //    }
            //    else
            //    {
            //        dpsl = Convert.ToDouble(this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[this.dataGridView1.Columns["数量"].Index].Value) - Convert.ToDouble(this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[this.dataGridView1.Columns["已排产量"].Index].Value);

            //    }

            //    double x1 = dpsl;
            //    double x2 = Convert.ToDouble(this.dataGridView1.CurrentCell.Value);
            //    int x3 = 0;
            //    if (x2 == 0)
            //    {
            //        x3 = 0;
            //    }
            //    else
            //    {

            //        x3 = Convert.ToInt32(x1 / x2) + 1;
            //    }

            //    WTLXPCCS xx = new WTLXPCCS(x1, x2, x3);
            //    j = new LXPCCS();
            //    j.DataSelected += new EventHandler(j_DataSelected);
            //    j.cdpccs += new LXPCCS.Cdpccs(textBox2_TextChanged);
            //    j.Show(this);


            //}
        }

        void j_DataSelected(object sender, EventArgs e)
        {
            //string strSql = null;
            //double pcsl = 0;
            //for (int k = 1; k <= j.pcts; k++)
            //{
            //    if (j.dpsl >= j.dbcl * k)
            //    {
            //        pcsl = j.dbcl;
            //    }
            //    else
            //    {
            //        pcsl = j.dpsl - j.dbcl * (k - 1);
            //    }

            //    if (pcsl > 0)
            //    {
            //        strSql = "INSERT INTO MOCTY(COMPANY,CREATOR,CREATE_DATE,FLAG,TY001,TY002,TY003,TY004,TY005,TY006,TY007,TY008,TY009,TY018,TY019) SELECT '','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','" + dataGridView1["工单别", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','" + dataGridView1["工单号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','" + this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex + k].Name.ToString() + "'," + pcsl.ToString() + ",0,0,0,'N',";
            //        strSql += "'" + dataGridView1["生产线", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','','" + dataGridView1["工序号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' WHERE NOT EXISTS(SELECT * FROM MOCTY WHERE TY001='" + dataGridView1["工单别", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY002='" + dataGridView1["工单号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TY003='" + this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex + k].Name.ToString() + "' AND TY009='" + dataGridView1["生产线", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "')";
                   
            //        try
            //        {
            //            if (db.ExecDataBySql1(strSql) > 0)
            //            {
            //                this.dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[this.dataGridView1.Columns["已排产量"].Index].Value = Convert.ToDouble(this.dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[this.dataGridView1.Columns["已排产量"].Index].Value) + pcsl;
            //                this.dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex + 1].Cells[this.dataGridView1.CurrentCell.ColumnIndex].Value = pcsl;

            //                string strSql1 = "INSERT INTO DATELOG SELECT '" + dataGridView1["工单别", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','" + dataGridView1["工单号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','" + this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].Name.ToString() + "','" + dataGridView1["生产线", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','" + dataGridView1["工序号", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "','新增'," + dataGridView1.CurrentCell.Value.ToString() + ",CONVERT(CHAR(20),GETDATE(),120),'" + PropertyClass.OperatorName + "'";
            //                try
            //                {

            //                    if (db.ExecDataBySql1(strSql1) > 0)
            //                    {

            //                    }
            //                    else
            //                    {

            //                    }
            //                }
            //                catch (Exception ex)
            //                {
            //                    MessageBox.Show(ex.Message, "软件提示");
            //                    throw ex;
            //                }
                        
            //            }
            //            else
            //            {

            //            }
            //        }
            //        catch (Exception ex)
            //        {
            //            MessageBox.Show(ex.Message, "软件提示");
            //            throw ex;
            //        }
            //    }
            //}
            //button1.PerformClick();
        }
    

        private void textBox2_Enter(object sender, EventArgs e)
        {
            //MessageBox.Show(this.textBox2.Height.ToString(), "提示");
            System.Windows.Forms.Button btn = new System.Windows.Forms.Button();//创建Buttonbtn    
            btn.Text = "...";//设置button文字    
            btn.Font = new System.Drawing.Font("Arial", 5);//设置文字格式    
            btn.Visible = true;//设置控件允许显示    

            btn.Width = this.textBox2.Height;//获取单元格高并设置为btn的宽    
            btn.Height = this.textBox2.Height;//获取单元格高并设置为btn的高    

            btn.Click += new EventHandler(btn_Click);//为btn添加单击事件 
            this.textBox2.Controls.Add(btn);
            btn.Location = new System.Drawing.Point(((this.textBox2.Right) -
                       (btn.Width)), this.textBox2.Height);//设置btn显示位置
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                gridControl1.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            //button1.PerformClick();
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
           // button1.PerformClick();
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            //if (e.RowIndex > -1 && e.ColumnIndex > this.dataGridView1.Columns["生产线"].Index)
            //{
            //    if (e.Button == MouseButtons.Right)
            //    {
            //        if (e.RowIndex >= 0)
            //        {
            //            //若行已是选中状态就不再进行设置
            //            if (dataGridView1.Rows[e.RowIndex].Selected == false)
            //            {
            //                dataGridView1.ClearSelection();
            //                dataGridView1.Rows[e.RowIndex].Selected = true;
            //            }
            //            //只选中一行时设置活动单元格
            //            if (dataGridView1.SelectedRows.Count == 1)
            //            {
            //                dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
            //            }
            //            //弹出操作菜单
            //            contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
            //        }
            //    }
            //}
        }

        private void 查看物料状况ToolStripMenuItem_Click(object sender, EventArgs e)
        {
           

        }

        private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            try
            {
                string x1 = "";
                string x2 = "";
                double x3 = 0;
                string x4 = "";
                string x5 = "";
                string x6 = "";
                string x7 = "";
                string x8 = "";

                x1 = gridView1.GetFocusedRowCellValue("工单别").ToString();
                x2 = gridView1.GetFocusedRowCellValue("工单号").ToString();
                x3 = Convert.ToDouble(this.gridView1.GetFocusedValue().ToString());
                x5 = gridView1.GetFocusedRowCellValue("工序号").ToString();
                x6 = gridView1.GetFocusedRowCellValue("生产线").ToString();
                x7 = this.gridView1.FocusedColumn.FieldName;
                x8 = gridView1.GetFocusedRowCellValue("工艺").ToString();

                for (int i = 0; i < contextMenuStrip1.Items.Count; i++)
                {

                    if (contextMenuStrip1.Items[i].Selected)
                    {

                        if (contextMenuStrip1.Items[i].Text.Trim() == "查看物料状况")
                        {

                            x4 = "查看物料状况";
                            MXCX xx = new MXCX(x1, x2, x3, x4, x8);
                            CHAXUN f = new CHAXUN();
                            f.cdgzzx += new CHAXUN.Cdgzzx(textBox2_TextChanged);
                            f.Show(this);

                        }

                        else if (contextMenuStrip1.Items[i].Text.Trim() == "查看排产状况")
                        {

                            x4 = "查看排产状况";

                            MXCX xx = new MXCX(x1, x2, x3, x4, x5);
                            CHAXUN f = new CHAXUN();
                            f.cdgzzx += new CHAXUN.Cdgzzx(textBox2_TextChanged);
                            f.Show(this);

                        }
                        else if (contextMenuStrip1.Items[i].Text.Trim() == "查看排产记录")
                        {

                            x4 = "查看排产记录";

                            MXCXA xx = new MXCXA(x1, x2, x7, x4, x5, x6);
                            CHAXUN f = new CHAXUN();
                            f.cdgzzx += new CHAXUN.Cdgzzx(textBox2_TextChanged);
                            f.Show(this);

                        }
                        else if (contextMenuStrip1.Items[i].Text.Trim() == "查看计划完成状况")
                        {

                            x4 = "查看计划完成状况";
                            MXCX xx = new MXCX(x1, x2, x3, x4, x5);
                            CHAXUN f = new CHAXUN();
                            f.cdgzzx += new CHAXUN.Cdgzzx(textBox2_TextChanged);
                            f.Show(this);

                        }
                    }

                }

            }
            catch
            {

            }
        }

       

        private void 删除排程计划ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (PropertyClass.jiaose != "计划员" && PropertyClass.jiaose != "计划主管" && PropertyClass.jiaose != "超级管理员" && PropertyClass.jiaose != "车间统计员")
            {
                MessageBox.Show("对不起,您没有修改权限", "软件提示");
                return;
            }
            string a = PropertyClass.gzzx.ToString();
            string b = gridView1.GetFocusedRowCellValue("工作中心").ToString();
            if (a.IndexOf(b) <= -1 || b == "")
            {
                MessageBox.Show("对不起,您没有对工作中心" + b + "进行排产的权限", "软件提示");
                return;
            }

            int[] rows = this.gridView1.GetSelectedRows();
            foreach (int row in rows)
            {
                string strSql = null;
                strSql = "DELETE FROM MOCTY WHERE TY001='" + this.gridView1.GetRowCellValue(row, "工单别").ToString() + "' AND TY002='" + this.gridView1.GetRowCellValue(row, "工单号").ToString() + "' AND TY019='" + this.gridView1.GetRowCellValue(row, "工序号").ToString() + "'";
                try
                {
                    if (db.ExecDataBySql1(strSql) > 0)
                    {

                        this.gridView1.SetRowCellValue(row, "已排产量", 0.00);
                        string strSql1 = "INSERT INTO DATELOG SELECT '" + this.gridView1.GetRowCellValue(row, "工单别").ToString() + "','" + this.gridView1.GetRowCellValue(row, "工单号").ToString() + "','" + this.gridView1.FocusedColumn.FieldName + "','" + this.gridView1.GetRowCellValue(row, "生产线").ToString() + "','" + this.gridView1.GetRowCellValue(row, "工序号").ToString() + "','删除',0,CONVERT(CHAR(20),GETDATE(),120),'" + PropertyClass.OperatorName + "'";
                        try
                        {

                            if (db.ExecDataBySql1(strSql1) > 0)
                            {

                            }
                            else
                            {

                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            throw ex;
                        }

                    }
                    else
                    {

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
            }
            MessageBox.Show("删除成功,请重新排产!", "软件提示");
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button1.PerformClick(); 
            }

        }

        private void textBox4_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button1.PerformClick();
            }
        }

        private void textBox5_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button1.PerformClick();
            }
        }

        private void textBox6_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button1.PerformClick();
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            button1.PerformClick();
        }

        private void button3_Click(object sender, EventArgs e)
        {
           
        }

        private void ZPCJH_Shown(object sender, EventArgs e)
        {
            TimeSpan ts = Convert.ToDateTime(textBox3.Text) - Convert.ToDateTime(textBox1.Text);
            int k = ts.Days;
            if (k >= 0)
            {
                //DataTable dt = new DataTable();//建立个数据表

                DateTime rq = Convert.ToDateTime(textBox1.Text);
                DevExpress.XtraGrid.Columns.GridColumn col = null;
                for (int i = 0; i <= k; i++)
                {
                    rq = Convert.ToDateTime(textBox1.Text).AddDays(i);
                    string str = rq.ToString("MM月dd日");
                    string str1 = rq.ToString("yyyyMMdd");
                    col = gridView1.Columns.AddVisible("scrq");
                    col.Name = "scrq";
                    col.FieldName = str1;
                    col.Caption = str;
                    col.MinWidth = 80;
                    col.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
                    col.SummaryItem.DisplayFormat = "{0:0.##}";
                    col.OptionsColumn.AllowEdit = true;
                    this.gridView1.Columns.Add(col);

                    //dt.Columns.Add(new DataColumn(Convert.ToString(str), typeof(string)));

                }
                //gridView1.PopulateColumns();
                //dataGridView1.DataSource = dt;
               // this.gridView1.BestFitColumns();//自动调整所有字段宽度
            }
            else
            {
            }
        }

        private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
        }

        private void gridControl1_Enter(object sender, EventArgs e)
        {
            //try
            //{
            //    this.gridControl1.Controls.Clear();//移除所有控件 
            //    if (gridView1.FocusedColumn.Name == "生产线")
            //    //if (e.ColumnIndex == 13)
            //    //判断单元格是否是"Company"列？    
            //    {
            //        System.Windows.Forms.Button btn = new System.Windows.Forms.Button();//创建Buttonbtn    
            //        btn.Text = "...";//设置button文字    
            //        btn.Font = new System.Drawing.Font("Arial", 5);//设置文字格式    
            //        btn.Visible = true;//设置控件允许显示    

            //        //btn.Width = this.gridControl1.GetCellDisplayRectangle(e.ColumnIndex,
            //        //                e.RowIndex, true).Height;//获取单元格高并设置为btn的宽    
            //        //btn.Height = this.gridView1.GetCellDisplayRectangle(e.ColumnIndex,
            //        //                e.RowIndex, true).Height;//获取单元格高并设置为btn的高    

            //        btn.Click += new EventHandler(btn_Click);//为btn添加单击事件    

            //        this.gridControl1.Controls.Add(btn);//gridView1中添加控件btn    

            //        //btn.Location = new System.Drawing.Point(((this.gridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Right) -
            //        //        (btn.Width)), this.gridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Y);//设置btn显示位置    
            //    }

            //    //if (e.ColumnIndex.Equals(this.gridView1.Columns["工作中心"].Index))
            //    ////if (e.ColumnIndex == 13)
            //    ////判断单元格是否是"Company"列？    
            //    //{
            //    //    System.Windows.Forms.Button btn = new System.Windows.Forms.Button();//创建Buttonbtn    
            //    //    btn.Text = "...";//设置button文字    
            //    //    btn.Font = new System.Drawing.Font("Arial", 5);//设置文字格式    
            //    //    btn.Visible = true;//设置控件允许显示    

            //    //    btn.Width = this.gridView1.GetCellDisplayRectangle(e.ColumnIndex,
            //    //                    e.RowIndex, true).Height;//获取单元格高并设置为btn的宽    
            //    //    btn.Height = this.gridView1.GetCellDisplayRectangle(e.ColumnIndex,
            //    //                    e.RowIndex, true).Height;//获取单元格高并设置为btn的高    

            //    //    btn.Click += new EventHandler(btnA_Click);//为btn添加单击事件    

            //    //    this.gridView1.Controls.Add(btn);//gridView1中添加控件btn    

            //    //    btn.Location = new System.Drawing.Point(((this.gridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Right) -
            //    //            (btn.Width)), this.gridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Y);//设置btn显示位置    
            //    //}
            //}
            //catch
            //{

            //}
        }

        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (PropertyClass.jiaose == "计划员" || PropertyClass.jiaose == "计划主管" || PropertyClass.jiaose == "超级管理员" || PropertyClass.jiaose == "车间统计员")
            {
                string a = PropertyClass.gzzx.ToString();
                string b = gridView1.GetFocusedRowCellValue("工作中心").ToString();
                if (a.IndexOf(b) <= -1 || b == "")
                {
                    MessageBox.Show("对不起,您没有对工作中心" + b + "进行排产的权限", "软件提示");
                    return;
                }
                if (this.gridView1.GetFocusedValue().ToString() != "")
                {
                    if (e.Column.Name=="生产线")
                    {
                        string strSql = null;
                        strSql = "SELECT MX001,MX003 FROM CMSMX  WHERE MX001 ='" + this.gridView1.GetFocusedValue().ToString() + "' AND MX002='" + gridView1.GetFocusedRowCellValue("工作中心").ToString() + "'";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                this.gridView1.SetFocusedRowCellValue("生产线名称",sdr1["MX003"].ToString());

                            }

                            if (sdr1.HasRows == false)
                            {
                                MessageBox.Show("该生产线不存在！", "软件提示");
                                this.gridView1.SetFocusedRowCellValue("生产线名称","");
                            }

                        }

                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                    }
                    if (e.RowHandle > -1 && e.Column.Name=="scrq")
                    {
                        string strSql = null;
                        double ypcl = 0;
                        double wpcl = 0;

                        strSql = "SELECT ISNULL(SUM(TY004),0) TY004 FROM MOCTY  WHERE TY001 ='" + gridView1.GetFocusedRowCellValue("工单别").ToString() + "' AND TY002='" + gridView1.GetFocusedRowCellValue("工单号").ToString() + "' AND TY019='" + gridView1.GetFocusedRowCellValue("工序号").ToString() + "'";
                        //MessageBox.Show(strSql,"123");
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                ypcl = Convert.ToDouble(sdr1["TY004"]);
                            }

                            if (sdr1.HasRows == false)
                            {
                                ypcl = 0;
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                        strSql = "SELECT ISNULL(SUM(TY004),0) TY004 FROM MOCTY  WHERE TY001 ='" + gridView1.GetFocusedRowCellValue("工单别").ToString() + "' AND TY002='" + gridView1.GetFocusedRowCellValue("工单号").ToString() + "' AND TY019='" + gridView1.GetFocusedRowCellValue("工序号").ToString() + "' AND TY003='" + this.gridView1.FocusedColumn.FieldName + "' AND TY009='" + gridView1.GetFocusedRowCellValue("生产线").ToString() + "'";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dqsl = Convert.ToDouble(sdr1["TY004"]);

                            }

                            if (sdr1.HasRows == false)
                            {
                                dqsl = 0;
                            }

                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }
                        wpcl = Convert.ToDouble(gridView1.GetFocusedRowCellValue("数量").ToString()) - ypcl;
                        if (ypcl + Convert.ToDouble(this.gridView1.GetFocusedValue().ToString()) - dqsl > Convert.ToDouble(gridView1.GetFocusedRowCellValue("数量").ToString()))
                        {
                            MessageBox.Show("最多只能排" + wpcl.ToString(), "提示");
                            this.gridView1.SetFocusedValue(wpcl);
                            //MessageBox.Show(row.ToString(), "提示");
                            //MessageBox.Show(column.ToString(), "提示");
                            //this.gridView1.CurrentCell = this.gridView1.Rows[row-1].Cells[column];
                            if (gridView1.GetFocusedRowCellValue("生产线") == DBNull.Value)
                            {
                                strSql = "UPDATE MOCTY SET TY004=" + this.gridView1.GetFocusedValue().ToString() + " WHERE TY001='" + gridView1.GetFocusedRowCellValue("工单别").ToString() + "' AND TY002='" + gridView1.GetFocusedRowCellValue("工单号").ToString() + "' AND TY003='" + this.gridView1.FocusedColumn.FieldName + "' AND TY009='' AND TY019='" + gridView1.GetFocusedRowCellValue("工序号").ToString() + "'";
                            }
                            else
                            {
                                strSql = "UPDATE MOCTY SET TY004=" + this.gridView1.GetFocusedValue().ToString() + " WHERE TY001='" + gridView1.GetFocusedRowCellValue("工单别").ToString() + "' AND TY002='" + gridView1.GetFocusedRowCellValue("工单号").ToString() + "' AND TY003='" + this.gridView1.FocusedColumn.FieldName + "' AND TY009='" + gridView1.GetFocusedRowCellValue("生产线").ToString() + "' AND TY019='" + gridView1.GetFocusedRowCellValue("工序号").ToString() + "'";
                            }
                            // MessageBox.Show(strSql, "提示");
                            try
                            {
                                if (db.ExecDataBySql1(strSql) > 0)
                                {
                                    //MessageBox.Show("删除成功！", "软体提示");
                                    this.gridView1.SetFocusedRowCellValue("已排产量", gridView1.GetFocusedRowCellValue("数量").ToString());

                                    string strSql1 = "INSERT INTO DATELOG SELECT '" + gridView1.GetFocusedRowCellValue("工单别").ToString() + "','" + gridView1.GetFocusedRowCellValue("工单号").ToString() + "','" + this.gridView1.FocusedColumn.FieldName + "','" + gridView1.GetFocusedRowCellValue("生产线").ToString() + "','" + gridView1.GetFocusedRowCellValue("工序号").ToString() + "','更新'," + this.gridView1.GetFocusedValue().ToString() + ",CONVERT(CHAR(20),GETDATE(),120),'" + PropertyClass.OperatorName + "'";
                                    try
                                    {

                                        if (db.ExecDataBySql1(strSql1) > 0)
                                        {

                                        }
                                        else
                                        {

                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(ex.Message, "软件提示");
                                        throw ex;
                                    }

                                }
                                else
                                {
                                    //MessageBox.Show("保存失败！", "软体提示");
                                    //return;
                                }
                            }

                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, "软件提示");
                                throw ex;
                            }

                            strSql = "INSERT INTO MOCTY(COMPANY,CREATOR,CREATE_DATE,FLAG,TY001,TY002,TY003,TY004,TY005,TY006,TY007,TY008,TY009,TY018,TY019) SELECT '','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','" + gridView1.GetFocusedRowCellValue("工单别").ToString() + "','" + gridView1.GetFocusedRowCellValue("工单号").ToString() + "','" + this.gridView1.FocusedColumn.FieldName + "'," + wpcl + ",0,0,0,'N',";
                            strSql += "'" + gridView1.GetFocusedRowCellValue("生产线").ToString() + "','','" + gridView1.GetFocusedRowCellValue("工序号").ToString() + "' WHERE NOT EXISTS(SELECT * FROM MOCTY WHERE TY001='" + gridView1.GetFocusedRowCellValue("工单别").ToString() + "' AND TY002='" + gridView1.GetFocusedRowCellValue("工单号").ToString() + "' AND TY003='" + this.gridView1.FocusedColumn.FieldName + "' AND TY009='" + gridView1.GetFocusedRowCellValue("生产线").ToString() + "' AND TY019='" + gridView1.GetFocusedRowCellValue("工序号").ToString() + "')";

                            try
                            {
                                if (db.ExecDataBySql1(strSql) > 0)
                                {
                                    this.gridView1.SetFocusedRowCellValue("已排产量", gridView1.GetFocusedRowCellValue("数量").ToString()); 

                                    string strSql1 = "INSERT INTO DATELOG SELECT '" + gridView1.GetFocusedRowCellValue("工单别").ToString() + "','" + gridView1.GetFocusedRowCellValue("工单号").ToString() + "','" + this.gridView1.FocusedColumn.FieldName + "','" + gridView1.GetFocusedRowCellValue("生产线").ToString() + "','" + gridView1.GetFocusedRowCellValue("工序号").ToString() + "','新增'," + this.gridView1.GetFocusedValue().ToString() + ",CONVERT(CHAR(20),GETDATE(),120),'" + PropertyClass.OperatorName + "'";
                                    try
                                    {

                                        if (db.ExecDataBySql1(strSql1) > 0)
                                        {

                                        }
                                        else
                                        {

                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(ex.Message, "软件提示");
                                        throw ex;
                                    }
                                }
                                else
                                {

                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, "软件提示");
                                throw ex;
                            }


                            return;

                        }

                        if (dqsl >= 0)
                        {

                            if (gridView1.GetFocusedRowCellValue("生产线") == DBNull.Value)
                            {
                                strSql = "UPDATE MOCTY SET TY004=" + this.gridView1.GetFocusedValue().ToString() + " WHERE TY001='" + gridView1.GetFocusedRowCellValue("工单别").ToString() + "' AND TY002='" + gridView1.GetFocusedRowCellValue("工单号").ToString() + "' AND TY003='" + this.gridView1.FocusedColumn.FieldName + "' AND TY009='' AND TY019='" + gridView1.GetFocusedRowCellValue("工序号").ToString() + "'";
                            }
                            else
                            {
                                strSql = "UPDATE MOCTY SET TY004=" + this.gridView1.GetFocusedValue().ToString() + " WHERE TY001='" + gridView1.GetFocusedRowCellValue("工单别").ToString() + "' AND TY002='" + gridView1.GetFocusedRowCellValue("工单号").ToString() + "' AND TY003='" + this.gridView1.FocusedColumn.FieldName + "' AND TY009='" + gridView1.GetFocusedRowCellValue("生产线").ToString() + "' AND TY019='" + gridView1.GetFocusedRowCellValue("工序号").ToString() + "'";
                            }
                            //MessageBox.Show(strSql,"提示");
                            try
                            {
                                if (db.ExecDataBySql1(strSql) > 0)
                                {
                                    if (gridView1.GetFocusedRowCellValue("已排产量") == DBNull.Value)
                                    {

                                        this.gridView1.SetFocusedRowCellValue("已排产量",Convert.ToDouble(this.gridView1.GetFocusedValue().ToString()));
                                    }
                                    else
                                    {
                                        this.gridView1.SetFocusedRowCellValue("已排产量", ypcl + Convert.ToDouble(this.gridView1.GetFocusedValue().ToString()) - dqsl);
                                        //MessageBox.Show("保存成功！", "软体提示");
                                    }
                                    string strSql1 = "INSERT INTO DATELOG SELECT '" + gridView1.GetFocusedRowCellValue("工单别").ToString() + "','" + gridView1.GetFocusedRowCellValue("工单号").ToString() + "','" + this.gridView1.FocusedColumn.FieldName + "','" + gridView1.GetFocusedRowCellValue("生产线").ToString() + "','" + gridView1.GetFocusedRowCellValue("工序号").ToString() + "','更新'," + this.gridView1.GetFocusedValue().ToString() + ",CONVERT(CHAR(20),GETDATE(),120),'" + PropertyClass.OperatorName + "'";
                                    try
                                    {

                                        if (db.ExecDataBySql1(strSql1) > 0)
                                        {

                                        }
                                        else
                                        {

                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(ex.Message, "软件提示");
                                        throw ex;
                                    }
                                }
                                else
                                {
                                    //MessageBox.Show("保存失败！", "软体提示");
                                    //return;
                                }
                            }

                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, "软件提示");
                                throw ex;
                            }
                        }

                        strSql = "INSERT INTO MOCTY(COMPANY,CREATOR,CREATE_DATE,FLAG,TY001,TY002,TY003,TY004,TY005,TY006,TY007,TY008,TY009,TY018,TY019) SELECT '','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','" + gridView1.GetFocusedRowCellValue("工单别").ToString() + "','" + gridView1.GetFocusedRowCellValue("工单号").ToString() + "','" + this.gridView1.FocusedColumn.FieldName + "'," + this.gridView1.GetFocusedValue().ToString() + ",0,0,0,'N',";
                        strSql += "'" + gridView1.GetFocusedRowCellValue("生产线").ToString() + "','','" + gridView1.GetFocusedRowCellValue("工序号").ToString() + "'  WHERE NOT EXISTS(SELECT * FROM MOCTY WHERE TY001='" + gridView1.GetFocusedRowCellValue("工单别").ToString() + "' AND TY002='" + gridView1.GetFocusedRowCellValue("工单号").ToString() + "' AND TY003='" + this.gridView1.FocusedColumn.FieldName + "' AND TY009='" + gridView1.GetFocusedRowCellValue("生产线").ToString() + "' AND TY019='" + gridView1.GetFocusedRowCellValue("工序号").ToString() + "')";
                        try
                        {
                            if (db.ExecDataBySql1(strSql) > 0)
                            {
                                if (gridView1.GetFocusedRowCellValue("已排产量") == DBNull.Value)
                                {
                                    this.gridView1.SetFocusedRowCellValue("已排产量", Convert.ToDouble(this.gridView1.GetFocusedValue().ToString()));
                                }
                                else
                                {
                                    this.gridView1.SetFocusedRowCellValue("已排产量", Convert.ToDouble(gridView1.GetFocusedRowCellValue("已排产量")) + Convert.ToDouble(this.gridView1.GetFocusedValue().ToString()));
                                }

                                string strSql1 = "INSERT INTO DATELOG SELECT '" + gridView1.GetFocusedRowCellValue("工单别").ToString() + "','" + gridView1.GetFocusedRowCellValue("工单号").ToString() + "','" + this.gridView1.FocusedColumn.FieldName + "','" + gridView1.GetFocusedRowCellValue("生产线").ToString() + "','" + gridView1.GetFocusedRowCellValue("工序号").ToString() + "','新增'," + this.gridView1.GetFocusedValue().ToString() + ",CONVERT(CHAR(20),GETDATE(),120),'" + PropertyClass.OperatorName + "'";
                                try
                                {

                                    if (db.ExecDataBySql1(strSql1) > 0)
                                    {

                                    }
                                    else
                                    {

                                    }
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.Message, "软件提示");
                                    throw ex;
                                }
                            }
                            else
                            {
                                //MessageBox.Show("保存失败！", "软体提示");
                                //return;
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            throw ex;
                        }
                        //finally
                        //{
                        //    sdr1.Close();
                        //}


                    }
                }
            }
            if (PropertyClass.jiaose != "计划员" && PropertyClass.jiaose != "计划主管" && PropertyClass.jiaose != "超级管理员" && PropertyClass.jiaose != "车间统计员")
            {
                MessageBox.Show("对不起,您没有修改权限", "软件提示");
                return;
            }
        }

        private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (Convert.ToString(e.Value) == "0")
                e.DisplayText = string.Empty;
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        private void 查看排产状况ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}